简体   繁体   English

多维数组和指向指针值的指针

[英]Multidimensional array and pointer to pointer value

Given the following: 给定以下内容:

int ar2[3][2];
int * pt;
int **p2;
p2 = &pt;
*p2 = ar2[0];

This is what my book says: 这就是我的书所说的:

*p2 is type pointer-to-int, making it compatible with ar2[0]. * p2是指向整数的指针,使其与ar2 [0]兼容。 Recall that ar2[0] is a pointer to its first element , art2[0][0], making ar2[0] type pointer-to-int also. 回想一下ar2 [0] 是指向其第一个元素 art2 [0] [0]的指针,使ar2 [0]的指针类型也指向int。

Is it everything true? 这是真的吗?

Yes. 是。

*p2 is type pointer-to-int, making it compatible with ar2[0]. * p2是指向整数的指针,使其与ar2 [0]兼容。

To make this easier to see, ar2 can be seen as a pointer to an array, being that array ar2[0]. 为了使这一点更容易理解,可以将ar2视为指向数组的指针,即该数组ar2 [0]。 Then, ar2+1 would be a pointer to ar2[1], and ar2+2 a pointer to ar2[2]. 那么,ar2 + 1将是指向ar2 [1]的指针,而ar2 + 2将是指向ar2 [2]的指针。

So, you could define int (* arp)[2] = ar2 , making explicit your reinterpretation of ar2 as a pointer to an array. 因此,您可以定义int (* arp)[2] = ar2 ,从而明确将ar2重新解释为指向数组的指针。

Recall that ar2[0] is a pointer to its first element, art2[0][0], making ar2[0] type pointer-to-int also. 回想一下ar2 [0]是指向其第一个元素art2 [0] [0]的指针,使ar2 [0]的指针类型也指向int。

Then, ar2[0]+1 would be a pointer to ar2[0][1]. 那么,ar2 [0] +1将是指向ar2 [0] [1]的指针。


This way, you can always reinterpret an array as a pointer to the first element of it, being it an integer, an array or even another pointer. 这样,您始终可以将数组重新解释为指向其第一个元素的指针 ,它可以是整数,数组甚至是另一个指针。

For instance, int * par[3] would be an array of 3 pointers, OR a pointer to the first of the 3 pointers ( par would be int ** and par[0] int * ). 例如, int * par[3]将是3个指针的数组,或者是指向3个指针中第一个的指针( par将是int **par[0] int * )。

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

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