简体   繁体   English

在以下声明中,使用指针有何区别?

[英]What is the difference between the usage of pointer in the following declarations?

char *array[size];
char (*aPointer)[size];

I'm pretty new to C++ and currently I'working on an assignment.And I noticed there are two different(maybe?) use of pointer. 我是C ++的新手,目前正在做一个赋值工作,我注意到有两种不同的(也许?)指针用法。 Can anyone please tell me what are the difference between them? 谁能告诉我他们有什么区别? Thank you 谢谢

char *array[size] declares an array of size pointers to char s. char *array[size]声明一个指向charsize指针数组。

char (*aPointer)[size] declares a pointer to an array of size char s. char (*aPointer)[size]声明一个指向size char s的数组的指针。

Just for the sake of completeness, there's also char *cPointer , which declares a pointer to a char , which could be the first one in an array. 为了完整起见,还有char *cPointer ,它声明了一个指向char的指针,该指针可以是数组中的第一个指针。 The difference between that and aPointer is a semantic one. 那和aPointer之间的区别是语义上的。 The memory layout will be identical. 内存布局将相同。 For example, aPointer + 1 will return a pointer that points to a location size bytes after the one pointed to by aPointer , while cPointer + 1 will return a pointer that points to a location one byte after the one pointed to by cPointer . 例如, aPointer + 1将返回一个指向位置的指针size字节的一个后通过指向aPointer ,而cPointer + 1将返回后的一个由指向指向的位置处的一个字节的指针cPointer

So char *array[size] declares something like this: 因此char *array[size]声明如下:

 array      +--------+
+------+    | a char |
| a[0] +--->+--------+
+------+
| a[1] +--->+--------+
+------+    | a char |
| ...  |    +--------+
+------+

Of course, each of the char s pointed to could be the first element of an array as well: 当然,每个指向的char也可以是数组的第一个元素:

 array      +--------+--------+--------+
+------+    | a char | a char |  ...   |
| a[0] +--->+--------+--------+--------+
+------+
| a[1] +--->+--------+--------+--------+
+------+    | a char | a char |  ...   |
| ...  |    +--------+--------+--------+
+------+

char (*aPointer)[size] declares something like this: char (*aPointer)[size]声明如下:

 aPointer
+--------+    +--------+
|        +--->+ a char |
+--------+    +--------+
              | a char |
              +--------+
              |  ...   |   aPointer + 1
              +--------+    +--------+
                        <---+        |
                            +--------+

char *cPointer could declare something like this: char *cPointer可以声明如下内容:

 cPointer
+--------+    +--------+
|        +--->+ a char |   cPointer + 1
+--------+    +--------+    +--------+
              | a char +<---+        |
              +--------+    +--------+
              |  ...   |
              +--------+

Note that in all of those examples, the pointers don't initially point to anything. 请注意,在所有这些示例中,指针最初都没有指向任何东西。 You have to point them at something yourself. 您必须自己将它们指向某处。

The first declares an array of size pointers to char . 第一个声明了一个指向charsize指针数组。 The second declares a pointer to an array of size elements of type char . 第二个声明了一个指向char类型的size元素数组的指针。

You can think of it as the [] operator binding more strongly than * . 您可以将其视为比*更强的[]运算符绑定。 Start reading declarations like this one at the identifier and work your way outwards. 开始在标识符处读取这样的声明,然后向外进行操作。 In the first case, we see the identifier array . 在第一种情况下,我们看到了标识符array Next to it we have [] as well as * . 旁边有[]* The [] binds more strongly than * , so it applies first. []绑定比*更牢固,因此它首先适用。 Thus, array is an array of something. 因此, array是事物的数组。 Next applies the * , so array is an array of pointers to something. 接下来应用* ,因此array是一个指向某物的指针数组。 The only thing that's left is the type specifier char , thus array is an array of pointers to char . 剩下的唯一是类型说明符char ,因此array是一个指向char的指针的数组。 The second case works analogously, only there we have parentheses which force the * to apply before the [] . 第二种情况类似地起作用,仅在这里有括号,迫使*[]之前应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM