Question 11
Back to Exercise
What is the output of the following C program?
#include <stdio.h>
int *slot(void) {
static int value;
value++;
return &value;
}
int main(void) {
int *a = slot();
int *b = slot();
printf("%d %d\n", a == b, *a);
return 0;
}
- A 1 2 Correct
- B 0 1
- C 1 1
- D 0 2
Correct Answer: Option A
There is only one value, at the same address for the whole program, so both pointers are equal. After two calls it holds 2. Returning its address is safe because its lifetime never ends.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!