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
Type
Typical size on a 64-bit system
char
1 byte
int, float
4 bytes
double
8 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 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.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/pointers/addresses-pointers-and-the-and-and-operators/ to read this page.