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:

  1. int arr[5] = {30, 50, 10}; initializes arr[0] = 30, arr[1] = 50, arr[2] = 10, arr[3] = 0, arr[4] = 0.
  2. ptr = &arr[0] + 1; makes ptr point to arr[1].
  3. a = *ptr; sets a = 50.
  4. (*ptr)++; increments arr[1] in place from 50 to 51.
  5. ptr++; advances ptr to point to arr[2], so *ptr is 10.
  6. a + (*ptr) + arr[1] evaluates to 50+10+51=11150 + 10 + 51 = 111.
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.