Pointers to pointers: add one statement inside fun so that j in main receives the address of local a (demonstration of double-pointer assignment).
#include
int main()
{
int j;
void fun(int*);
fun(&j);
return 0;
}
void fun(int *k)
{
int a = 10;
/ Add a statement here */
}
-
A**k = a;
-
Bk = &a;
-
C*k = &a;
-
D&k = *a;
-
Ek = a;
Answer
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:
- Main declares int j; then calls fun(&j); therefore inside fun, k points to j.
- Local variable in fun: int a = 10; located on the stack of fun.
- Goal: store the address of a into j via k.
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;