Pointers & Memory
![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)
Answer & solution
Official answer: 111
Step through it (12 steps)
Array initialization yields arr=[30,50,10,0,0]; omitted entries are zero-initialized. Pointer arithmetic &arr[0]+1 advances by one int element, so ptr initially points to arr[1], whose value is 50. The assignment a=*ptr copies 50 into a.
The parentheses in (*ptr)++ make the increment apply to the pointed-to value. Hence arr[1] becomes 51 while ptr stays at arr[1]. The copied variable a remains 50. The next statement ptr++ instead increments the pointer itself, moving it to arr[2], whose value is 10.
At the print, the three terms are a=50, *ptr=10, and arr[1]=51. Their sum is 50+10+51=111. The operations occur in separate statements, so their sequencing is unambiguous. Confusing (*ptr)++ with *ptr++ would mix up value mutation and pointer advancement and give the wrong trace. Answer: 111.