MCS-201 · Unit 7

Pointers

Section 1 of 7

Block 2 · Unit 7Pointers10 min · Section 1 of 7

7.1Addresses, Pointers and the & and * Operators

Covers IGNOU MCS-201 §7.2, 7.3

To understand pointers you first need a picture of memory. This section explains how variables are placed in memory, what an address is, and the two operators that work with addresses.

Memory and addresses

A computer’s memory is a long row of bytes, and each byte has a number called its address. When you declare a variable, the compiler reserves enough consecutive bytes for its type; the address of the first of those bytes is the address of the variable.

So there are always two things to keep apart: the pointer itself, which holds an address, and the object it points to, which holds a value.

Sizes of variables and of pointers

TypeTypical size on a 64-bit system
char1 byte
int, float4 bytes
double8 bytes
Any pointer (int *, char *, double *)8 bytes

A pointer holds an address, so every pointer type has the same size on a given system, whatever it points to.

The address-of operator &

The unary operator & gives the address of its operand: &qty is “the address of qty”. It can be applied to variables and array elements, but not to constants, expressions such as (a + b), or register variables.

The indirection operator *

The unary operator *, called the indirection operator or dereference operator, gives the object stored at an address: *p is “the value at the address held in p”. The two operators undo each other, so *(&qty) is simply qty.

Why pointers are used

  • To let a function change variables that belong to its caller.
  • To pass large objects, such as arrays and structures, without copying them.
  • To work with arrays and strings efficiently.
  • To use memory obtained while the program runs (dynamic memory allocation).
  • To build linked data structures such as lists and trees (MCS-208).

Key takeaways

  • Every variable has an address: the number of its first byte in memory.
  • A pointer is a variable that holds an address.
  • & gives an address; * gives the object at an address.
  • All pointer types have the same size (8 bytes on 64-bit systems); print addresses with %p.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What does a pointer variable store?

Show answer

Answer: B. The memory address of another object

A pointer’s own value is an address. The value of the other variable is reached through that address with *.

Question 2 · multiple choice

If int n = 12;, what is the value of *(&n)?

Show answer

Answer: C. 12

&n is the address of n, and * gives the object at that address, which is n itself, 12.

Question 3 · multiple choice

On a typical 64-bit system, what is sizeof(char *)?

Show answer

Answer: D. 8

A pointer holds an address, which is 8 bytes on 64-bit systems whatever type it points to. 1 is the size of a char, not of a pointer to it.

Question 4 · multiple choice

Which conversion should be used to print an address with printf?

Show answer

Answer: A. %p

%p is the conversion for pointers. %d and %u are for 4-byte integers and cannot hold a 64-bit address.

Question 5 · multiple choice

To which of these can the & operator be applied?

Show answer

Answer: C. marks[3]

An array element is an object in memory with an address. Constants, expression results and register variables have no address that can be taken.

Question 6 · short answer

Explain the difference between &x and *p when int x = 40; and int *p = &x;, and state the value of each.

Show worked answer

&x uses the address-of operator: it gives the memory address where x is stored (some address such as 0x7ffd…, different on each run). *p uses the indirection operator: p holds the address of x, so *p is the object at that address, which is x; its value is 40. In short, & goes from a variable to its address, and * goes from an address back to the variable.

↑ ↓ move↵ openTab switch type