Question discussion

What is the output of the following code? #include

359429Medium DSSSB TGT Computer Science 0 replies 0 views
0
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;
}
  • ACompile time error
  • B20
  • C10
  • DGarbage value
Correct answer: B

Explanation

func changes only its local copy of the pointer p, so ptr still points to x and prints 20.

Discussion 0

Nobody has replied yet. Start the discussion!