LearnFrenzy
Sign in Take a free test

Functions · Find the Error · Question 8

Error: function pointer with the wrong type Answer, explanation & discussion

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

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

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

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links