Question 7
Back to Exercise
The program below has a bug. Which statement correctly describes it?
#include <stdio.h>
typedef int (*Handler)(int);
static int greet(const char *name) {
return printf("hi %s\n", name);
}
int main(void) {
Handler h = greet;
h(5);
return 0;
}
- A static functions cannot be assigned to pointers
- B greet takes a const char *, but Handler points to functions taking an int – the types do not match Correct
- C h(5) should be (*h)(5)
- D There is no bug
Correct Answer: Option B
A function pointer must match the function's parameter and return types exactly. Calling greet through a Handler would pass an int where a string pointer is expected. The compiler rejects the assignment.
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!