Question:medium


Show Hint

In C, when you pass a pointer to a function, any modification to the value pointed to by the pointer will affect the original variable. Ensure you understand how pointers work when analyzing such programs.
Updated On: Jan 30, 2026
Show Solution

Correct Answer: 25

Solution and Explanation

Program Analysis:

We can determine the output by examining the scope and the effect of the pointer indirection within the function call.


Step 1: Memory Initialization in main 

Two integer variables are allocated in the stack: a (20) and b (25).A pointer z is then initialized to store the memory address of a ($\&a$).


Step 2: Function Scope Analysis (foo)

The function foo(int *p, int x) is invoked.

  • Argument 1: p is a copy of z, meaning p now also holds the address of a.
  • Argument 2: x is a local copy of b (value 25). Changes to x would not affect b.

 


Step 3: Pointer Indirection and Dereferencing

Inside the function, the instruction *p = x; is executed.This does not change the pointer itself, but rather the value stored at the address p is holding.Since p points to a, the memory location of a is overwritten with the value of x (25).


Final Answer:

When control returns to main, the printf retrieves the current value from a's memory address. Output = 25

Was this answer helpful?
0

Top Questions on Programming in C