简体   繁体   English

c中的`int(*)(int)`对于类型是什么意思?

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

I have this C code (inspired by a test) 我有这个C代码(灵感来自测试)

#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. 我特别感兴趣的是foo的价值以及它的类型。 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? 什么是int (*)(int)以及如何在没有参数的情况下调用函数名称会给我一些这种类型的东西?

Without the function call, foo evaluates to a pointer to a function that takes an int and returns an int . 如果没有函数调用, foo计算一个指向函数的指针,该函数接受一个int并返回一个int That is the long description of int (*)(int) . 这是int (*)(int)的长描述。

You are not calling foo . 你不是在叫foo To call foo , place (argument) after the name. 要调用foo ,请在名称后面放置(argument) 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 . 实际上,你获取foo地址 ,它的类型为int (*)(int) (指向函数的指针获取一个整数参数并返回一个int)并将其发送到printf

printf then complains because you are trying to tell it to print an int , but are giving it a function pointer. printf然后抱怨,因为你试图告诉它打印一个int ,但给它一个函数指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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