c++ - 复杂的 C 声明

标签 c++ c function-pointers declaration

我刚刚在网上浏览了一些代码,发现了这个:

float * (*(*foo())[SIZE][SIZE])()

如何阅读此声明?阅读此类复杂的声明是否有一套特定的规则?

最佳答案

我有一段时间没有这样做了!

foo开始往右走。

float * (*(*foo())[SIZE][SIZE])()

foo is a function with no arguments...

因为有右括号,所以不能正确。向左走:

float * (*(* foo())[SIZE][SIZE])()

foo is a function with no arguments returning a pointer

不能再往左走,所以让我们划掉括号再往右走

float * (*(* foo())[SIZE][SIZE])() float * (*(* foo())[SIZE][SIZE])() float * (*(* foo())[SIZE][SIZE])()

foo is a function with no arguments returning a pointer to an array of SIZE arrays of SIZE ...

到达右括号,再次向左到达指针符号:

float * (*(* foo())[SIZE][SIZE])()

foo is a function with no arguments returning a pointer to an array of SIZE arrays of SIZE pointers to ...

再次左括号,所以我们越过它并再次向右走:

float *( *(* foo())[SIZE][SIZE])() float *( *(* foo())[SIZE][SIZE])()

foo is a function with no arguments returning a pointer to an array of SIZE arrays of SIZE pointers to a function with no arguments...

从头到尾

float * ( *(* foo())[SIZE][SIZE])()

foo is a function with no arguments returning a pointer to an array of SIZE arrays of SIZE pointers to a function with no arguments returning a pointer to float


而写的人,请教他使用typedef:

// Function that returns a pointer to float
typedef float* PFloatFunc ();

// Array of pointers to PFloatFunc functions
typedef PFloatFunc* PFloatFuncArray2D[SIZE][SIZE];

// Function that returns a pointer to a PFloatFuncArray2D
PFloatFuncArray2D* foo();

关于c++ - 复杂的 C 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15111526/

相关文章:

c++ - VS2012 : 'nmake' is not recognized as an internal or external command

c++ - 游戏 map 对象属性的位域与多态性

c - 在c中实现ls命令

C++:如何将指针传递给另一个类的成员函数?

c++ - 如何在不使用 sqrt 的情况下检查整数是否为完美平方

c++ - `#pragma pack`在网络编程中有什么用?

c - 在 typedef 之外使用修饰符或类型 'unsigned' [MISRA 2012 指令 4.6,建议]

c - 数组运算符 "-"(连字符或破折号)在 C 语言中的含义和作用是什么?

c++ - 为什么这里会出现 "the adress of bool will always evaluate as true"?

c++ - 函数指针调用不编译