ProgrammingVery hardIBM · Deloitte pattern

What is printed?

What is printed?
#include <stdio.h>
void set(int **pp) {
    static int x = 99;
    *pp = &x;
}
int main(void) {
    int y = 5;
    int *p = &y;
    set(&p);
    printf("%d", *p);
}
  1. A. 99
  2. B. 5
  3. C. Undefined behaviour
  4. D. 0

Correct answer: A. 99

Passing &p lets set reach into main's actual pointer variable through one more level of indirection, so *pp = &x really does redirect main's p to the static x. *p now reads 99.

More C Programming questions

Exams that ask this topic