What is the output of the following code? #include <stdio.h>
What is the output of the following code? #include <stdio.h>
void func(int *p) {
int q = 10;
p = &q;
}
int main() {
int x = 20, *ptr = &x;
func(ptr);
printf("%d\n", *ptr);
return 0;
}
Show answerHide answer
Correct answer: (B) 20
Explanation
C passes the pointer by value, so p = &q changes only the local copy in func. ptr still points to x, and 20 is printed.