Similar questions 5 more →
Tracing pointer reassignment in C
Pointers & Memory

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.

![Question 62, GATE 2025: Find the integer output of this C fragment:
int a; int arr[5]={30,50,10}; int *ptr;
ptr=&arr[0]+1;
a=*ptr;
(*ptr)++;
ptr++;
printf("%d",a+(*ptr)+arr[1]);](../../../assets/questions/CS-2025-S2-Q62-1.webp)
![Question 36, GATE 2024: C code declares double a[2]={20.0,25.0}; sets p=a and q=p+1; and prints "%d,%d" with (int)(q−p), (int)(*q−*p). Which output? A 4,8; B 1,5; C 8,5; D 1,8.](../../../assets/questions/CS-2024-S2-Q36-1.webp)
![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.](../../../assets/questions/CS-2022-Q21-1.webp)
![Question 10, GATE 2021: ANSI C declares int arr[4][5] and initializes arr[i][j]=10*i+j for 0≤i<4,0≤j<5. It prints *(arr[1]+9) with %d. Offered outputs: A 14; B 20; C 24; D 30. Explain the intended result and the language-standard issue.](../../../assets/questions/CS-2021-S2-Q20-1.webp)

![Question 22, GATE 2020: Find the output of this C program:
#include <stdio.h>
int main(void) {
int a[4][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20}};
printf("%d\n", *(*(a+**a+2)+3));
return 0;
}](../../../assets/questions/CS-2020-Q32-1.webp)
![Question 24, GATE 2019: A C program initializes int arr[]={1,2,3,4,5,6,7,8,9,0,1,2,5}; and int *ip=arr+4; then prints ip[1] using the %d conversion. Find the integer displayed.](../../../assets/questions/CS-2019-Q34-1.webp)






![Question 55, GATE 2017: C program includes stdio.h and string.h, then executes char *c="GATECSIT2017"; char *p=c; printf("%d",(int)strlen(c+2[p]-6[p]-1));. What output is intended, and what caveat applies to the printed expression?](../../../assets/questions/CS-2017-S2-Q65-1.webp)

![Question 62, GATE 2015: Assume x address=2000 decimal and unsigned int occupies four bytes. Code: int main(){unsigned int x[4][3]={{1,2,3},{4,5,6},{7,8,9},{10,11,12}};printf("%u, %u, %u",x+3,*(x+3),*(x+2)+3);}. A 2036,2036,2036; B 2012,4,2204; C 2036,10,10; D 2012,4,6.](../../../assets/questions/CS-2015-S1-Q62-1.webp)
![Question 43, GATE 2015: What is the intended printed output of this C program? Also distinguish that output from its portability limitation.
#include <stdio.h>
int main() { static int a[]={10,20,30,40,50}; static int *p[]={a,a+3,a+4,a+1,a+2}; int **ptr=p; ptr++; printf("%d%d",ptr-p,**ptr); }
There is no separator between the two decimal conversions.](../../../assets/questions/CS-2015-S3-Q43-1.webp)

![Question 22, GATE 2011: What does this literal C fragment print? The initial capital in Char is as printed.
Char c[] = "GATE2011";
char *p = c;
printf("%s", p + p[3] - p[1]);
(A) GATE2011
(B) E2011
(C) 2011
(D) 011](../../../assets/questions/CS-2011-Q32-1.webp)




![Question 2, GATE 2003: Given int *A[10], B[10][10]; consider I. A[2], II. A[2][3], III. B[1], IV. B[2][3]. Which expressions can be left-hand sides of assignments without compile-time errors, assuming type-compatible right-hand sides? A. I, II and IV only; B. II, III and IV only; C. II and IV only; D. IV only.](../../../assets/questions/CS-2003-Q2-1.webp)


