我刚刚在网上浏览了一些代码,发现了这个:
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/