LearnFrenzy
Sign in Take a free test

Typedef · Find the Error · Question 7

Error: wrong function signature for a typedef'd pointer Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

The program below has a bug. Which statement correctly describes it?

main.c
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links