user3282276
user3282276

Reputation: 3804

What does `int (*)(int)` in c mean with regard to type?

I have this C code (inspired by a test)

#include <stdio.h>

int foo (int n)
{
    static int s = 0;
    return s += n;
}

int main()
{
    int y;
    int i;

    for (i=0; i<5; i++) {
        y= foo(i);
    }

    printf("%d\n", foo);

    return 0;
}

and I am specifically interested in the value of foo and what type it has. The compiler gives me this warning

test.c:18:16: warning: format specifies type 'int' but the argument has type
      'int (*)(int)' [-Wformat]

but I'm not really sure what that means. What is an int (*)(int) and how does calling the function name with no arguments give me something of this type?

Upvotes: 2

Views: 1307

Answers (2)

owacoder
owacoder

Reputation: 4873

You are not calling foo. To call foo, place (argument) after the name. As it is, you take the address of foo, which is of type int (*)(int) (pointer to function taking one integer argument and returning an int) and send it to printf.

printf then complains because you are trying to tell it to print an int, but are giving it a function pointer.

Upvotes: 0

R Sahu
R Sahu

Reputation: 206577

Without the function call, foo evaluates to a pointer to a function that takes an int and returns an int. That is the long description of int (*)(int).

Upvotes: 11

Related Questions