← 👉 Pointers & Arrays
Subtopic 1 of 5

3.1Pointers: address, dereference, aliasing

The idea

A pointer is a variable that stores an address. &x gives the address of x; *p follows the address to the object.

Think of it like this: A pointer is a house address written on a card. The card is not the house; * means "go to that house".

Why it exists

Pointers let several names refer to the same object (aliasing), which is why one assignment can change what another name reads.

How it works

  1. p = &x: p now holds x's address.
  2. *p = 10: go to that address and store 10, so x is 10.
  3. p = &z[1]: p now holds a different address; x is unaffected.
  4. *p++ reads *p and then moves p; (*p)++ changes the pointed-to value.

Worked example: *(&z[0] + 1) += 3

&z[0] + 1 is the address of z[1], so the statement is z[1] += 3, turning 11 into 14. With the earlier *p = 10 the program prints 10, 10, 14.

Now play with it

  • →Run the aliasing program and watch the gold-highlighted variables.
  • →In *p++ versus (*p)++, notice that only one of them moves p.

p first points to x, so *p = 10 changes x. Then p moves to z[1]. *(&z[0] + 1) is z[1] as well.

step 1 of 9
1#include <stdio.h>
2int main() {
3 int x = 1, z[2] = {10, 11};
4 int *p = NULL;
5 p = &x;
6 *p = 10;
7 p = &z[1];
8 *(&z[0] + 1) += 3;
9 printf("%d, %d, %d\n", x, z[0], z[1]);
10 return 0;
11}

The gold line is the statement about to run.

Call stack (top first)
main()
no variables yet
Output

Check yourself

int a[] = {5, 6}; int *p = a; int x = *p++; What are x and *p?

Remember: & = address, * = follow it; *p++ moves the pointer, (*p)++ changes the value.
Exam notes (the dense version)

A pointer variable holds an address. &x is the address of x and *p is the object p points to. Two pointers to the same object are aliases: writing through one changes what the other reads. *p++ means *(p++) (use the old pointer, then advance), whereas (*p)++ increments the pointed-to value. A NULL pointer must never be dereferenced.

Previous year questions: Pointers & Arrays (11)

PYQs are grouped by topic, so this is the full set for Pointers & Arrays; try the ones that match this subtopic. Open all 11 on the topic page.