0 · Recall Warm-up
Answer from memory first. Answers are in the Answer Vault at the bottom.
- What is
**qifint **q = &p;andp = &a;? from Lesson 1.4.1 - What value does a global int start with if not initialized? from Lesson 1.3.2
- What does
(double)7 / 2give? from Lesson 1.1.3
1 · The Hook
🎣
In Lesson 1.3.1 a swap function swapped only its own copies. Now you can hand it the addresses of the two variables. It still gets copies, but copies of addresses lead straight back to the originals, like giving a plumber the address of your house instead of a photo of your tap.
2 · Predict First
🤔
What does this print?
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
int main(void)
{
int x = 1, y = 2;
swap(&x, &y);
printf("%d %d\n", x, y);
return 0;
}
Make a guess in your head. The answer opens Section 3.
3 · Core Concept
💡
Predict First — the answer
Output
2 1
swap receives &x and &y. *a and *b are x and y themselves, so the swap reaches main’s variables.
Passing addresses
C still passes everything by value, but if the value is an address, the function can follow it. The pattern:
- the parameter has pointer type (
int *a); - the caller passes an address (
&x); - the function reads and writes through
*a.
This simulates call by reference (passing the variable itself), which C does not have directly.
Returning several results
return gives one value; pointer parameters can carry more results back:
void divmod(int a, int b, int *q, int *r)
{
*q = a / b;
*r = a % b;
}
int main(void)
{
int quo, rem;
divmod(47, 5, &quo, &rem);
printf("%d %d\n", quo, rem);
return 0;
}
Output
9 2
The pointer itself is still a copy
Inside the function, the pointer parameter is a local copy. Making it point elsewhere (p = q;) changes only the copy; the caller’s pointer and the variable it pointed to are unaffected. Only writes through *p reach the caller.
Changing the caller’s pointer
To make a function change which variable the caller’s pointer points to, pass the address of that pointer (an int **):
int g = 99;
void redirect(int **pp)
{
*pp = &g;
}
int main(void)
{
int x = 1;
int *p = &x;
redirect(&p);
*p = *p + 1;
printf("%d %d\n", x, g);
return 0;
}
Output
1 100
4 · Visual
swap(&x, &y): the parameters a and b hold copies of the addresses, so *a and *b reach main’s x and y.
void f(int *p, int *q) { p = q; *p = 2; } called as f(&i, &j), i = 0, j = 1
before p = q: p --> i (0) q --> j (1)
after p = q: p --> j q --> j (only f's copy p moved)
after *p = 2: j becomes 2; i untouched -> main prints "0 2"
Re-pointing a parameter does not affect the caller; writing through it does.
5 · Worked Examples
Example 1 Easy
What does this print?
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
int main(void)
{
int x = 1, y = 2;
swap(&x, &y);
printf("%d %d\n", x, y);
return 0;
}
Solution
t = *a = 1; *a = *b sets x = 2; *b = t sets y = 1.
Output
2 1
Example 2 Medium
What does this print?
void upd(int *a, int b)
{
*a = *a + b;
b = b * 2;
}
int main(void)
{
int x = 5, y = 3;
upd(&x, y);
upd(&y, x);
printf("%d %d\n", x, y);
return 0;
}
Solution
upd(&x, 3): x = 5 + 3 = 8 (b is a copy; doubling it does nothing). upd(&y, 8): y = 3 + 8 = 11.
Output
8 11
Example 3 GATE
What does this print?
void f(int *p, int *q)
{
p = q;
*p = 2;
}
int main(void)
{
int i = 0, j = 1;
f(&i, &j);
printf("%d %d\n", i, j);
return 0;
}
Solution
p receives &i, q receives &j. p = q moves f’s copy p to point at j. *p = 2 writes 2 into j. i is never touched.
Output
0 2
6 · GATE Traps
⚠️
- Forgetting & at the call (
swap(x, y)with pointer parameters) passes ints as addresses: undefined behavior. - Re-pointing a parameter (
p = q) changes only the local copy. - To change the caller’s pointer, pass its address (pointer to pointer).
- C has no true call by reference; it is simulated by passing addresses by value.
- Passing the same address twice (
t(&a, &a)) makes both parameters alias one variable; arithmetic swaps then destroy it.
8 · Memory Hook
🧠
“Send the address, not the photo.” A copy of an address still leads to the real house.
9 · Connect the Dots
Lessons 1.4.1–1.4.2 used pointers to single variables. An address is a number, and C lets you do arithmetic on it, which matters as soon as several values sit next to each other in memory. Next lesson answers: what does p + 1 mean, and why does it not simply add 1 to the address?
10 · Self-Test
GATE format. Try every question before opening the vault.
Q1MCQ2 marksOutput
What does this print?
void upd(int *a, int b)
{
*a = *a + b;
b = b * 2;
}
int main(void)
{
int x = 5, y = 3;
upd(&x, y);
upd(&y, x);
printf("%d %d\n", x, y);
return 0;
}
- (A)
8 6 - (B)
8 19 - (C)
8 11 - (D)
5 3
Q2MCQ1 markConcept
How does C let a function change a variable of its caller?
- (A) by passing the variable by reference automatically
- (B) by declaring the parameter static
- (C) it cannot
- (D) by passing the variable’s address and writing through it
Q3NAT1 markdivmod
After divmod(47, 5, &q, &r) from the lesson, what is q + r?
Numerical answer: ________
Q4MCQ2 marksAliasing
What does this print?
void t(int *p, int *q)
{
*p = *p + *q;
*q = *p - *q;
*p = *p - *q;
}
int main(void)
{
int a = 4, b = 9;
t(&a, &b);
printf("%d %d ", a, b);
t(&a, &a);
printf("%d\n", a);
return 0;
}
- (A)
4 9 0 - (B)
9 4 0 - (C)
9 4 9 - (D)
9 4 4
Q5MCQ2 marksRe-pointing
What does this print?
void f(int *p, int *q)
{
p = q;
*p = 2;
}
int main(void)
{
int i = 0, j = 1;
f(&i, &j);
printf("%d %d\n", i, j);
return 0;
}
- (A)
2 2 - (B)
0 1 - (C)
0 2 - (D)
2 1
11 · Rule Card
- Simulated call by reference: param type T *, call with &var, use *param.
- Pointer parameters return extra results.
- The pointer parameter itself is a copy: p = q inside does not affect the caller.
- Change the caller’s pointer via T ** (pass &p).
- Same address passed twice → parameters alias one variable.
12 · Concept Ledger
Unlocked in this lesson. From here on, these terms may be used.
call by reference (simulated)output parameteraliasingpointer parameter is a copy
13 · Answer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Recall Warm-up
- a (the int that p points to).
-
- 3.5.
Self-Test
Q1 Answer: (C)2 marks
Only *a writes reach main.
Output
8 11
- (A) is wrong: The second call adds the new x = 8 to y.
- (B) is wrong: b = b * 2 changes only the copy.
- (D) is wrong: x and y are changed through pointers.
Re-read: Lesson 1.4.2 — Simulating call by reference with pointers (swap)
Q2 Answer: (D)1 mark
C passes values; an address value lets the function reach the variable.
- (A) is wrong: C has only call by value.
- (B) is wrong: static changes lifetime, not passing.
- (C) is wrong: Pointers make it possible.
Re-read: Lesson 1.4.2 — Simulating call by reference with pointers (swap)
Q3 Answer: 111 mark
q = 9, r = 2: 11.
Re-read: Lesson 1.4.2 — Simulating call by reference with pointers (swap)
Q4 Answer: (B)2 marks
The first call swaps (9 4). In t(&a, &a) both pointers are a: a = 9 + 9 = 18; a = 18 − 18 = 0; a = 0 − 0 = 0.
Output
9 4 0
- (A) is wrong: The first call swaps a and b.
- (C) is wrong: The aliasing call zeroes a.
- (D) is wrong: Both parameters alias a, so a becomes 0.
Re-read: Lesson 1.4.2 — Simulating call by reference with pointers (swap)
Q5 Answer: (C)2 marks
p now points to j; *p = 2 writes j.
Output
0 2
- (A) is wrong: Only j is written.
- (B) is wrong: j becomes 2.
- (D) is wrong: i is never written.
Re-read: Lesson 1.4.2 — Simulating call by reference with pointers (swap)