ACompressed recap
🧾
&x is x’s address; int *p = &x; stores it; *p is x. In int *p, q; only p is a pointer. All pointers are the same size (4 bytes here). NULL points nowhere; following NULL or garbage is undefined. int **q = &p; gives two levels.
Passing addresses lets a function change its caller’s variables (simulated call by reference); the pointer parameter itself is still a copy, so re-pointing it does nothing outside; to change the caller’s pointer pass a pointer to it.
Pointer arithmetic counts elements: p + k adds k × sizeof(*p) bytes; q − p gives elements; *p++ moves the pointer, (*p)++ changes the value.
BMerged rule card
1.4.1 & and *; pointer declaration; NULL; pointer to pointer
- &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.
1.4.2 Simulating call by reference with pointers (swap)
- 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.
1.4.3 Pointer arithmetic and why it scales by type size
- p + k = address + k × sizeof(*p); p++ moves one element.
- q − p = number of elements between (same row).
- Allowed: ptr ± int, ptr − ptr, comparisons. Not: ptr + ptr, ×, ÷.
- *p++ = *(p++); *++p = *(++p); ++*p = ++(*p); (*p)++ changes the value, p stays.
- One past the end may be computed/compared, never dereferenced.
CInterleaved GATE-style questions
Q1MCQ2 marksOutput
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);
- (A)
2 10 10 - (B)
2 9 2 - (C)
3 10 3 - (D)
3 9 3
Q2MCQ1 markSwap
Which prototype lets swap(&x, &y) exchange two ints?
- (A) void swap(int *a, int *b)
- (B) void swap(int **a, int b)
- (C) void swap(int a, int b)
- (D) int swap(int a, int b)
Q3NAT1 markScaling
A short *s (2-byte shorts) holds 5000. What does s + 7 hold?
Numerical answer: ________
Q4MCQ2 marksOutput
What does this print?
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;
}
- (A)
2 99 - (B)
1 100 - (C)
2 100 - (D)
1 99
Q5MCQ1 markNULL
What happens on int *p = NULL; *p = 5;?
- (A) compile-time error
- (B) p’s target becomes 5
- (C) p becomes 5
- (D) undefined behavior
Q6MSQ2 marksExpressions
With int x = 3, *p = &x;, which expressions leave x equal to 4 (each starting fresh)?
- (A)
*p++ - (B)
++*p - (C)
*p = *p + 1 - (D)
(*p)++
Q7NAT2 marksOutput
What does this print? (enter the first number)
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;
}
Numerical answer: ________
Q8MCQ1 markSizes
On a 64-bit machine, sizeof(char *) is:
- (A) 4
- (B) 8
- (C) 1
- (D) depends on the string
Q9MCQ2 marksRe-pointing
Inside void f(int *p) { int k = 7; p = &k; }, after f(&a) with a = 1, a is:
- (A) 7
- (B) 1
- (C) undefined
- (D) 0
DAnswer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: (A)2 marks
*r = q re-points p to y; **r + 1 increments y.
Output
2 10 10
- (B) is wrong: y is incremented.
- (C) is wrong: x is not touched.
- (D) is wrong: p was moved to y before the increment.
Re-read: Lesson 1.4.1 — & and *; pointer declaration; NULL; pointer to pointer
Q2 Answer: (A)1 mark
Pointer parameters receive the addresses.
- (B) is wrong: Wrong types for &x and &y.
- (C) is wrong: Values cannot reach the caller.
- (D) is wrong: One return value cannot swap two variables.
Re-read: Lesson 1.4.2 — Simulating call by reference with pointers (swap)
Q3 Answer: 50141 mark
5000 + 14 = 5014.
Re-read: Lesson 1.4.3 — Pointer arithmetic and why it scales by type size
Q4 Answer: (B)2 marks
redirect makes main’s p point to g; *p + 1 then updates g.
Output
1 100
- (A) is wrong: p no longer points to x.
- (C) is wrong: x is untouched.
- (D) is wrong: g is incremented.
Re-read: Lesson 1.4.2 — Simulating call by reference with pointers (swap)
Q5 Answer: (D)1 mark
Dereferencing NULL is undefined (usually a crash).
- (A) is wrong: It compiles.
- (B) is wrong: There is no target.
- (C) is wrong: *p = 5 does not change p.
Re-read: Lesson 1.4.1 — & and *; pointer declaration; NULL; pointer to pointer
Q6 Answer: (B) (C) (D)2 marks
*p++ moves p and leaves x alone.
- (A) is wrong: *p++ increments the pointer, not x.
Re-read: Lesson 1.4.3 — Pointer arithmetic and why it scales by type size
Q7 Answer: 92 marks
47 / 5 = 9 (remainder 2).
Output
9 2
Re-read: Lesson 1.4.2 — Simulating call by reference with pointers (swap)
Q8 Answer: (B)1 mark
All pointers are 8 bytes there.
- (A) is wrong: That is the 32-bit size.
- (C) is wrong: The pointer, not the char, is measured.
- (D) is wrong: Pointer size is fixed.
Re-read: Lesson 1.4.1 — & and *; pointer declaration; NULL; pointer to pointer
Q9 Answer: (B)2 marks
Only f’s copy p is re-pointed; nothing is written to a.
- (A) is wrong: No write through the original address.
- (C) is wrong: a is well defined.
- (D) is wrong: a keeps its value.
Re-read: Lesson 1.4.2 — Simulating call by reference with pointers (swap)