有人可以解释一下声明数组和指针混合的规则吗?
以下是我所发现的。但是,我不明白为什么int *p[10]
手段an allocation of 10 consecutive memory blocks (each can store data of type int *)
,而int (*p)[10]
手段declaring a pointer to an array of 10 consecutive memory blocks (each can store data of type int)
。这些法规背后的规则是什么?
int p[10]; //10 consecutive memory blocks (each can store data of type int) are allocated and named as p
int *p[10]; //10 consecutive memory blocks (each can store data of type int *) are allocated and named as p
int (*p)[10]; //p is a pointer to an array of 10 consecutive memory blocks (each can store data of type int)
目前,我对规则的猜测是:
xxx[10]
将被解释为please allocating 10 memory blocks in which each blocks may store xxx
。因此,int *p[10]
将导致10 memory blocks in which each can store data of type int *
:
(*p)
将首先由于解释()
,这将导致指向某处的指针。因此,int (*p)[10]
将导致a pointer to an array of 10 consecutive memory blocks (each can store data of type int)
。
对不起,我的英语不好。希望你能理解我的意思。任何帮助表示赞赏!非常感谢你!
C对类型使用中缀表示法。一个由10个int组成的数组的类型为int [10]
,但是从数组类型派生的任何内容的派生都在theint
和the之间[10]
(不像某些其他语言那样在末尾)。
如果我们声明具有此类型的变量,则不是int [10] arr;
,而是int arr [10];
同样,指向10个int数组的指针不是int [10] *
,但它是int (*) [10]
。
()
需要的原因是因为int * [10]
它将是10的数组int *
。在()
有分手的效果int
和*
,所以它不能被解析为类型int *
。除此以外,他们“没有任何意义”。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句