Question

#include <stdio.h>
void foo(int *p, int x){
    *p=x;
}
int main(){
    int *z;
    int a = 20, b = 25;
    z = &a;
    foo(z,b);
    printf("%d",a);
    return 0;
}

The output of the given C program is __________. (Answer in integer)

Official answer and solution
Official answer
25
From
Official GATE 2025 answer key

Solution

Trace how passing pointer z = &a to foo modifies a via dereferencing *p = x.

  1. In main(), a is initialized to 20, b to 25, and pointer z is assigned the address of a (z = &a).
  2. Calling foo(z, b) passes p = &a and x = 25.
  3. Inside foo, *p = x; writes 25 directly to the memory location of a.
  4. printf("%d", a); prints 25.
Final answer

25

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 1 (CS1)
Question
34 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.