Question:

What is the output of the following C code?

  void foo(int *p, int x) {
      *p = x;
  }

  void main() {
      int *z;
      int a = 20, b = 25;
      z = a;  // Incorrect: Should be z = a;
      foo(z, b);
      printf("%d", a);
  }
  

Issue: The statement z = a; is invalid because a is an integer, and z is a pointer.

Show Hint

<div>In C, when a pointer is passed to a function, any changes made to the value pointed by the pointer will affect the original variable in the calling function.</div>
Updated On: Feb 14, 2025
  • 20 

  • 25

  • 30 

  • Undefined

Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Step 1: Understanding the Code

  • Initially, a is set to 20, and b is set to 25. z is a pointer to a.
  • The function foo(z, b) modifies the value of a through the pointer z, assigning a the value of b (25).

Step 2: Conclusion

  • After the function call, the value of a becomes 25, which is printed.

Thus, the correct answer is (B).

Was this answer helpful?
0
0