我一直对C类型符号的工作原理有些困惑。 我无法访问Google,而Bing的搜索结果令人失望。
例如:int *(*)[]
是什么意思?我已经知道它是指向整数指针数组的指针(我想),但为什么呢?特别是,我不明白方括号的作用; 是的,int **[]
将是指向指针的指针数组,但为什么()
会改变它?
int **[] -> int **a[];
a[] //[] has higher precedence than *, so `a` is an array
*a[] //this array contains pointers
**a[] //which dereference to pointers
int **a[]; //which dereference to int
因此,int**[]
是指向指向int
的指针的指针数组的类型。
对于另一种类型,我们得到:
int *(*)[] -> int *(*a)[];
*a //a is a pointer
(*a) //(precedence control, only)
(*a)[] //which dereferences to an array
*(*a)[] //which contains pointers
int *(*a)[]; //which dereference to int
因此,int*(*)[]
是指向指针数组的指针类型。
正如您所看到的,括号的作用是在 []
之前选择第一个 *
运算符。后者具有更高的优先级,因此如果需要指向数组的指针,则需要引入括号。
有三个运算符与类型声明相关,了解它们的优先级很重要:
High precedence:
[] array subscript declares an array
() function call declares a function
Low precedence:
* dereference operator declares a pointer
*
的优先级低于 ()
或 []
,因此您需要添加额外的括号来声明指向数组或函数的指针。int *a[]; //array of pointers, as a cast: `(int*[])`
int (*a)[]; //pointer to an array, as a cast: `(int(*)[])`
int *a(); //function returning a pointer, as a cast: `(int*())`
int (*a)(); //pointer to a function returning an `int`, as a cast: `(int(*)())`
一旦你理解了这个原则,C语言中的任何类型表达式都不会再让你感到困惑。
int *(*name)[]
。 - KamilCukvoid foo(int *(*)[]);
- pmg#define IS_INT_ARRAY(x) _Generic(&(x), int(*)[]: true, default: false)
为例。 - Lundin