GATE / CS ARCHIVE

SIMILAR QUESTIONS / PROGRAMMING IN C

Trace pointer rebinding separately from object writes

6 questions solved the same way, newest first. Back to Programming in C

How these are solved

Use a storage-cell diagram, bind each pointer to an object, and distinguish assignments to the pointer from assignments through it. Each program is a short straight-line or single-call trace whose final values depend on that distinction.

2 marks · NAT
Report issue

Tracing pointer reassignment in C

Pointers & Memory

Question 60, GATE 2026: Find the printed c for this statement sequence in main: int *ptr,a,b,c; a=5; b=11; c=20; ptr=&a; *ptr=c; ptr=&c; a=*(&b); c=*ptr-a; printf("%d",c);. Assume successful compilation and execution.
Tap to enlarge
Answer & solution

Official answer: 9

Step through it (14 steps)

Givens and goal

Find the printed c for this statement sequence in main: int *ptr,a,b,c; a=5; b=11; c=20; ptr=&a; *ptr=c; ptr=&c; a=*(&b); c=*ptr-a; printf("%d",c);. Assume successful compilation and execution.

Approach

Track both the pointer target and the object values after every assignment.

Step-by-step solution

1. Initially a=5,b=11,c=20. The assignment ptr=&a stores a’s address in ptr; it does not change any integer value.
2. Then *ptr=c writes the current value of c into the object pointed to by ptr, which is a. The state becomes a=20,b=11,c=20, with ptr still pointing to a.
3. Next ptr=&c redirects the pointer to c without copying any old pointed-to value. The expression *(&b) is simply b, so a=*(&b) sets a=11. Now a=11,b=11,c=20 and ptr points to c.
4. In c=*ptr-a, the right-hand side reads the old value c=20 through ptr and subtracts a=11. The assignment therefore stores c=9, and printf prints 9.
5. This assignment is well-defined: the old c is read to calculate its new value. The crucial distinction is between changing ptr itself and assigning through *ptr to the current target.

Original paper, page 40 · Answer key

1 mark · NAT
Report issue

Following a pointer argument back to its object

Pointers & Memory

Question 34, GATE 2025: C function foo(int *p,int x) executes *p=x. In main, a=20,b=25, pointer z is assigned &a, then foo(z,b) is called and a is printed. What integer is printed?
Tap to enlarge
Answer & solution

Official answer: 25

Approach: Distinguish copying an argument from modifying the object that a copied pointer designates.
Initially a contains 20 and b contains 25. The assignment z=&a makes z hold the address of a. On calling foo(z,b), the parameter p receives a copy of that same address, while x receives the value 25.
Inside foo, the expression *p is the integer object at p’s address, which is main’s a. Therefore *p=x stores 25 into a. Returning from foo discards its local parameter objects but does not undo the write to a.
The subsequent printf of a prints 25. C still passes arguments by value: p itself is copied, but the copied address can be dereferenced to change an object owned by the caller. If the function had assigned only p or x, that would be a different operation; the dereferencing assignment is decisive.

Original paper, page 34 · Answer key

1 mark · MCQ
Report issue

Pointers & Memory

Question 21, GATE 2022: Find the output of this ANSI C main body: int x=1,z[2]={10,11}; int *p=NULL; p=&x; *p=10; p=&z[1]; *(&z[0]+1)+=3; printf("%d, %d, %d\n",x,z[0],z[1]); return 0; (stdio.h included). A 1,10,11; B 1,10,14; C 10,14,11; D 10,10,14.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Initially x=1 and z={10, 11}. Assigning 10 through p=&x changes x to 10. After p points to z[1], the expression *(&z[0]+1) denotes that same second element; adding 3 changes it to 14. z[0] stays 10. The printed values are 10, 10, 14, D.

Pointer reassignment changes what p refers to; it does not copy values between x and z. After *p=10, x remains 10 even after p moves. Array pointer arithmetic advances by one int, so &z[0]+1 is &z[1], not a byte address or z[0].

Original paper, page 17 · Answer key

1 mark · MCQ
Report issue

Pointers & Memory

Question 14, GATE 2017: Consider C code: void printxy(int x,int y){ int *ptr; x=0; ptr=&x; y=*ptr; *ptr=1; printf("%d,%d",x,y); }. What does printxy(1,1) print? A:0,0. B:0,1. C:1,0. D:1,1.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Step through it (10 steps)

1. At function entry the local parameter values are x=1,y=1. The assignment x=0 changes x, and ptr=&x makes ptr point to the storage occupied by x.

2. The expression *ptr reads that storage, currently 0, so y=*ptr copies 0 into y. This is a value copy, not a lasting connection between y and x.

3. The next statement *ptr=1 writes into x through the pointer. Now x=1 while y remains 0. The output is 1,0, option C. C function arguments are passed by value; the original call literals do not constrain these local assignments.

Original paper, page 6 · Answer key

1 mark · NAT
Report issue

Functions & Parameter Passing

Question 12, GATE 2016: C-like code: void f(int* p,int m){m=m+5; *p=*p+m; return;} void main(){int i=5,j=10; f(&i,j); printf("%d",i+j);} Find the printed value under the intended ordinary integer execution.
Tap to enlarge
Answer & solution

Official answer: 30.0 : 30.0

Our answer: 30

The value parameter m changes from 10 to 15 locally. Writing through p updates i from 5 to 20, while caller j stays 10. Hence i+j=30. Routine library declarations and the intended void-main extension are assumed.

Passing &i copies i’s address, not its value, into p. Dereferencing p consequently changes the caller’s i. Passing j copies 10 into the separate local variable m, so changing m cannot change j. The final addition is 20+10, not 20+15 or the original 5+10.

Original paper, page 28 · Answer key

1 mark · MCQ
Report issue

Pointer parameters passed by value

Pointers & Memory

Question 11, GATE 2010: What does this C program print?
#include <stdio.h>
void f(int *p, int *q) { p=q; *p=2; }
int i=0, j=1;
int main(void) { f(&i,&j); printf("%d %d\n",i,j); return 0; }
Options: (A) 2 2; (B) 2 1; (C) 0 1; (D) 0 2.
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Initially i=0 and j=1. The local parameter assignment p=q redirects p to j, without altering i or the caller’s pointer arguments. Then *p=2 writes j=2. The printed values are 0 2, option D.

C passes the pointer values by value. At entry p contains &i and q contains &j; p=q copies &j into the local pointer p. It does not copy j into i. The later dereference acts on the new pointee j. Distinguish p=q, which redirects a pointer, from *p=*q, which would copy an integer.

Original paper, page 3