简体   繁体   中英

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?

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) .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM