Question
Consider the following C program:
#include <stdio.h>
int main(){
int a;
int arr[5] = {30,50,10};
int *ptr;
ptr = &arr[0] + 1;
a = *ptr;
(*ptr)++;
ptr++;
printf("%d", a + (*ptr) + arr[1]);
return 0;
}
The output of the above program is ___________. (Answer in integer)
Official answer and solution
- Official answer
- 111
- From
- Official GATE 2025 answer key
Solution
Trace ptr, a, and the elements of arr across each pointer operation.
Tracing the execution step by step:
int arr[5] = {30, 50, 10};initializesarr[0] = 30, arr[1] = 50, arr[2] = 10, arr[3] = 0, arr[4] = 0.ptr = &arr[0] + 1;makesptrpoint toarr[1].a = *ptr;setsa = 50.(*ptr)++;incrementsarr[1]in place from50to51.ptr++;advancesptrto point toarr[2], so*ptris10.a + (*ptr) + arr[1]evaluates to .
Final answer
111
Common trap
Forgetting that (*ptr)++ modified arr[1] to 51.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2025 Computer Science and Information Technology - Session 2 (CS2)
- Question
- 62 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.