Difficulty: Medium
Correct Answer: k = &a;
Explanation:
Introduction / Context:
This exercise demonstrates how to write through a pointer-to-pointer parameter in order to modify a pointer variable in the caller. The function fun receives the address of j (type int) so it can assign a target address into j.
Given Data / Assumptions:
Concept / Approach:
To change a caller’s pointer through a function parameter, pass a pointer to that pointer. Then assign through the double-pointer: k = &a; This writes the address value into j. Note: this demonstrates mechanics but leaves j dangling after fun returns because a is a local variable.
Step-by-Step Solution:
k is int; k is the caller’s int (j).&a is the address of the local int a.Assign *k = &a; which sets j to point at a.After fun returns, a ceases to exist → j becomes a dangling pointer (unsafe to dereference).
Verification / Alternative check:
To avoid a dangling pointer, allocate a dynamically: int *p = malloc(sizeof *p); *p = 10; *k = p; and later free in the caller.
Why Other Options Are Wrong:
**k = a assigns an int into a pointer variable. k = &a changes only the local parameter, not j, and also types mismatch. &k = *a is illegal. *k = a assigns value 10 to j (type mismatch).
Common Pitfalls:
Confusing the levels of indirection; ignoring lifetime of local variables when storing their addresses into caller pointers.
Final Answer:
*k = &a;
Discussion & Comments