如何理解“typedef int(xxx)(int yyy);”?

typedef int (xxx)(int yyy); 似乎定义了一个名为xxx的函数指针,它指向一个带有整数参数yyy的函数。

但我无法理解语法……任何人都可以给出一个很好的解释吗?


我找到了typedef int xxx(int yyy); 仍然有效。 他们之间有什么区别?

这定义了一个函数类型,而不是函数指针类型。

使用typedef的模式是它修改任何声明,使得它不会声明对象,而是声明对象所具有的类型的别名。

这完全有效:

 typedef int (xxx)(int yyy); // Note, yyy is just an unused identifier. // The parens around xxx are also optional and unused. xxx func; // Declare a function int func( int arg ) { // Define the function return arg; } 

具体而言,C和C ++语言不允许使用typedef名称作为函数定义中的整个类型。

是的, typedef int (xxx)(int yyy);typedef int xxx(int yyy); 它定义了一个函数类型。 您可以在第156页C 11标准草案N1570中找到示例。 从该页面引用,

All three of the following declarations of the signal function specify exactly the same type, the first without making use of any typedef names.

  typedef void fv(int), (*pfv)(int); void (*signal(int, void (*)(int)))(int); fv *signal(int, fv *); pfv signal(int, pfv); 

如果在声明T x有一些声明符T x ,则T x(t1 p1, t2 p2)表示具有参数p1,p2的函数,返回与声明器x之前具有的相同类型

声明符周围的括号表示首先在括号内应用修饰符

在您的情况下,括号内没有修饰符。 这意味着它们没有必要。

函数原型意味着在某个地方有一个具有此签名的函数,其名称为Blah

具有函数原型的Typedef意味着让我们给函数签名起一个名字 。 这并不意味着具有此签名的任何函数都存在。 此名称可用作类型。 例如:

 typedef int xxx(int yyy); xxx *func_ptr; // Declaration of a variable that is a pointer to a function. xxx *func2(int p1); // Function that returns a pointer to a function.