CS Atlas GATE 2027
PDS course
0 / 103 complete

PDS/Phase 1 · Programming in C

Lesson 1.4.1 · Item 21 of 103

& and *; pointer declaration; NULL; pointer to pointer

Read at your own pace. Try the questions before opening the answers.

On this page

0 · Recall Warm-up

Answer from memory first. Answers are in the Answer Vault at the bottom.

  • When a function returns, what happens to its automatic locals? from Lesson 1.3.3
  • Can void f(int x) { x = 9; } change the caller’s variable passed to it? from Lesson 1.3.1
  • What is 12 & 10? from Lesson 1.1.2

1 · The Hook

🎣

Instead of carrying a heavy parcel to a friend, you give them a slip with your house address. With the address they can walk to the parcel, look at it, even repack it. A pointer is that slip: a variable that holds the memory address of another variable.

2 · Predict First

🤔

What does this print?

int x = 10;
int *p = &x;
*p = 20;
printf("%d %d\n", x, *p);

Make a guess in your head. The answer opens Section 3.

3 · Core Concept

💡

Predict First — the answer

Output

20 20

p holds the address of x, so *p is x. Storing 20 through *p changes x itself.

The address-of operator &

Every variable lives at some address (Lesson 0.1.2). &x gives that address. (Do not confuse it with the bitwise & between two values.)

Pointer variables

int *p; declares p as a pointer to int: a variable whose value is the address of an int. p = &x; stores x’s address in p; we say p points to x.

The pointer’s type matters: an int * knows that the thing at the address is an int (4 bytes). A pointer itself is an address, so all pointers here have the same size, 4 bytes on our 32-bit machine (8 on a 64-bit one).

int x = 7;
double d = 2.5;
char c = 'A';
int *pi = &x;
double *pd = &d;
char *pc = &c;
printf("%u %u %u\n", sizeof(pi), sizeof(pd), sizeof(pc));
printf("%d %f %c\n", *pi, *pd, *pc);

Output

4 4 4
7 2.500000 A

Dereferencing with *

In an expression, *p means “the variable p points to”. Reading *p reads x; assigning *p = 20 changes x. The star in a declaration (int *p) says “p is a pointer”; the star in an expression (*p) follows the pointer.

Two pointers, one variable

Copying a pointer copies the address, so both pointers then refer to the same variable:

int a = 1, b = 2;
int *p = &a, *q = &b;
*p = *p + *q;
q = p;
*q = *q * 10;
printf("%d %d\n", a, b);

Output

30 2

*p + *q = 1 + 2 = 3 is stored into a. Then q = p makes q point to a too, so *q = *q × 10 sets a to 30; b is untouched.

NULL and bad pointers

NULL is a special pointer value meaning “points to nothing” (it compares equal to 0). Following a NULL pointer, an uninitialized pointer, or a pointer to a variable that no longer exists, is undefined behavior (typically a crash). Always give a pointer a valid address or NULL before use.

Pointer to pointer

A pointer is a variable too, so it has an address. int **q = &p; makes q a pointer to a pointer to int. Then *q is p and **q is the int that p points to.

int a = 5;
int *p = &a;
int **q = &p;
**q = *p + 10;
printf("%d %d %d\n", a, *p, **q);

Output

15 15 15

Printing addresses

printf("%p", (void *)p) prints an address (in hexadecimal). Its value changes from run to run and machine to machine, which is why GATE questions draw addresses as given numbers instead.

4 · Visual

Diagram: memory · 15 · 1000 · a (int) · 1000 · 1004 · p (int *) · 1004 · 1008 · q (int **)

int a = 5; int *p = &a; int **q = &p; **q = *p + 10; — p holds a’s address, q holds p’s address. Follow the arrows: **q reaches a.

q                p                a
+------+         +------+         +------+
| 1008 | ------> | 1000 | ------> |  15  |
+------+         +------+         +------+
 at 1008          at 1004          at 1000          (address under each box)

 q  = 1004        *q  = p = 1000         **q = a = 15
 &a = 1000        &p  = 1004             &q  = 1008

One star follows one arrow.

5 · Worked Examples

Example 1 Easy

What does this print?

int a = 3, b = 7;
int *p = &a;
*p = b;
p = &b;
*p = 1;
printf("%d %d\n", a, b);

Solution

p → a: *p = b stores 7 in a. Then p → b: *p = 1 stores 1 in b.

Output

7 1

Example 2 Medium

On our 32-bit machine, what does this print, and what changes on a 64-bit machine?

int x = 3;
int *p, q;
p = &x;
q = 4;
printf("%u %u\n", sizeof(p), sizeof(q));

Solution

In int *p, q; the star belongs to p only: p is a pointer, q is a plain int. Here both are 4 bytes.

Output

4 4

Compiled for 64-bit, pointers grow to 8 bytes but int stays 4: the output becomes 8 4.

Example 3 GATE

What does this print?

int x = 2, y = 9;
int *p = &x, *q = &y;
int **r = &p;
*r = q;
**r = **r + 1;
printf("%d %d %d\n", x, y, *p);

Solution

r points to p. *r = q changes p itself: now p points to y. Then **r = **r + 1 adds 1 to what p points to: y becomes 10. x is untouched, and *p is y = 10.

Output

2 10 10

6 · GATE Traps

⚠️

  • int *p, q; makes only p a pointer; q is an int.
  • The * in a declaration says “pointer”; the * in an expression follows the pointer.
  • Following NULL or an uninitialized pointer is undefined behavior.
  • Pointer size depends on the machine (4 bytes here, 8 on 64-bit), not on what it points to.
  • Assigning one pointer to another makes them point to the same variable; it does not copy the variable.
  • *r = q with r of type int ** changes the pointer that r points to, not an int.

8 · Memory Hook

🧠

“& asks where, * goes there.” &x is the house address; *p is walking to the house. Two stars = two walks.

9 · Connect the Dots

Lesson 1.3.1 showed that a function only gets copies, and Lesson 1.3.3 that the caller’s variables live in a different frame. A pointer is an address that reaches any variable, wherever it lives. Next lesson answers: by passing addresses instead of values, can a function finally change its caller’s variables?

10 · Self-Test

GATE format. Try every question before opening the vault.

Q1MCQ1 markOutput

What does this print?

int x = 4;
int *p = &x;
*p = *p * *p;
printf("%d\n", x);
  • (A) 4
  • (B) an address
  • (C) 16
  • (D) 8

Q2MCQ1 markDeclarations

In int *a, b, *c; which are pointers?

  • (A) a and c
  • (B) a, b and c
  • (C) b only
  • (D) a only

Q3NAT1 markSizes

On a machine with 8-byte pointers and 4-byte ints, what is sizeof(int *)?

Numerical answer: ________

Q4MSQ2 marksPointers

With int a = 1; int *p = &a; int **q = &p;, which expressions have the value 1?

  • (A) *q == p
  • (B) *p
  • (C) **q
  • (D) q == &a

Q5MCQ2 marksOutput

What does this print?

int a = 1, b = 2;
int *p = &a, *q = &b;
*p = *p + *q;
q = p;
*q = *q * 10;
printf("%d %d\n", a, b);
  • (A) 3 2
  • (B) 3 20
  • (C) 30 2
  • (D) 30 20

11 · Rule Card

  • &x = address of x; int *p = &x; p points to x.
  • *p in an expression is the variable p points to (read or write).
  • Declaration star belongs to one name: int *p, q; → q is int.
  • All pointers have the same size (4 here, 8 on 64-bit).
  • NULL = points to nothing; dereferencing NULL/garbage = undefined.
  • int **q = &p; *q is p; **q is *p.

12 · Concept Ledger

Unlocked in this lesson. From here on, these terms may be used.

address-of &pointerpointer type (int *)dereference *points toNULLpointer to pointer%p

13 · Answer Vault

Answer Vault

Try the questions first. Open when you are ready to check.

Recall Warm-up

  • Its frame is popped and they disappear.
  • No: x is a copy (call by value).

Self-Test

Q1 Answer: (C)1 mark

*p is x: x = 4 × 4 = 16.

Output

16
  • (A) is wrong: x is changed through p.
  • (B) is wrong: x is printed, not p.
  • (D) is wrong: It multiplies, not adds.

Re-read: Lesson 1.4.1 — & and *; pointer declaration; NULL; pointer to pointer

Q2 Answer: (A)1 mark

The * attaches to each name separately.

  • (B) is wrong: b has no *.
  • (C) is wrong: b is a plain int.
  • (D) is wrong: c has its own *.

Re-read: Lesson 1.4.1 — & and *; pointer declaration; NULL; pointer to pointer

Q3 Answer: 81 mark

All pointers are addresses: 8.

Re-read: Lesson 1.4.1 — & and *; pointer declaration; NULL; pointer to pointer

Q4 Answer: (A) (B) (C)2 marks

q holds p’s address, not a’s.

  • (D) is wrong: q == &p, not &a.

Re-read: Lesson 1.4.1 — & and *; pointer declaration; NULL; pointer to pointer

Q5 Answer: (C)2 marks

a = 3, then q points to a and a becomes 30.

Output

30 2
  • (A) is wrong: a is multiplied by 10 through q.
  • (B) is wrong: q no longer points to b.
  • (D) is wrong: b is never changed.

Re-read: Lesson 1.4.1 — & and *; pointer declaration; NULL; pointer to pointer

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.