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);
}- A. 99
- B. 5
- C. Undefined behaviour
- 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
- What is printed? c #include <stdio.h> int main(void) { int a[5] = {1, 2, 3, 4, 5}; int p = a + 4; printf("%d %d", p[-2], (p - 4)); }
- Assume no padding is needed. What is printed? c #include <stdio.h> struct P { int x; int y; }; int main(void) { struct P arr[3]; struct P…
- What is printed? c #include <stdio.h> int main(void) { int a[3] = {1, 2, 3}; int p = a; printf("%d ", p++); printf("%d ", p); }
- What is printed? c #include <stdio.h> int main(void) { int a[4] = {10, 20, 30, 40}; int p = a, end = a + 4; int sum = 0; while (p != end)…
- Assume a 4-byte int. What is printed? c #include <stdio.h> int main(void) { int a[2][3]; int (p)[3] = a; p++; printf("%ld", (long)p…
- What is printed? Assume sizeof(int) == 4. c int a[4] = {1, 2, 3, 4}; int p = a; printf("%d ", (p++)); printf("%d ", (++p)); printf("%d"…