Question 8
Back to Exercise
The program below has a problem. Which statement correctly describes it?
#include <stdio.h>
int add(int a, int b) { return a + b; }
int main(void) {
int (*f)(int) = add;
printf("%d\n", f(2));
return 0;
}
- A f must be declared with *f(int)
- B add takes two ints, but f points to a function taking one int – the types do not match Correct
- C Function pointers cannot be initialised
- D There is no error; it prints 2
Correct Answer: Option B
A function pointer must match the function's full type. Calling add through int (*)(int) would pass one argument where two are expected. Declare int (*f)(int, int) = add;.
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!