简体   繁体   English

C ++中的指针数组

[英]Array of pointers in c++

如果iptr是数组指针,则**(iptr+3)*iptr[3]之间没有任何区别。

不,而且令人惊讶地(或不是),它也等效于*(3[iptr])

there are nothing same between the **(iptr+3) and *iptr[3] since **(iptr+3)*iptr[3]之间没有什么相同,因为

*iptr[3] is the pointer representing the element having the address no-3 in the array *iptr[3]是表示数组中地址为3的元素的指针

**(iptr+3) and it's like if iptr is 3 than its **(6) **(iptr+3) ,就像iptr比其**(6)大3

To the compiler, no, there is no difference. 对于编译器,不,没有区别。 Both forms end up accessing the same element. 两种形式最终都访问相同的元素。

However, if you're writing code for the compiler, you've got the wrong audience. 但是,如果您正在为编译器编写代码,那么您的受众是错误的。 Your code should be written as if the next person to maintain it is a psychopath who knows where you live. 您的代码应该写成好像下一个要维护它的人是知道您住的地方的精神病患者。

In other words, code for readability and maintainability, and let the compiler itself worry about optimisations (a) . 换句话说,代码具有可读性和可维护性,并且让编译器本身担心优化(a) To that end, I always prefer the array[index] form over the *(array+index) one since the former clarifies intent: the accessing of an array element. 为此,我总是更喜欢array[index]形式而不是*(array+index)形式,因为前者明确了意图:访问数组元素。


(a) Of course, as with most rules, there are exceptions. (a)当然,与大多数规则一样,也有例外。 But, as with most exceptions, they're few and far between, so that advice stands. 但是,与大多数例外情况一样,它们之间的关系很少而且相去甚远,因此该建议是有效的。

It's the same due to pointer arithmetic. 由于指针运算,它是相同的。

Suppose that you have a custom type 假设您有一个自定义类型

typedef struct
{
     /* add some parameters here */
} MyStruct;

And then create a vector of this type 然后创建这种类型的向量

MyStruct xMyStructArray[20];

When you want to access to, for example, the third element, you can do in one of following ways: 例如,当您要访问第三个元素时,可以通过以下方式之一进行操作:

MyStruct xValue = xMyStructArray[2]; // index starts from zero
MyStruct xValue = xMyStructArray + 2;

xMyStructArray alone is seen as a pointer to the first array element. 仅xMyStructArray被视为指向第一个数组元素的指针。

xMyStructArray == &(xMyStructArray[0]) xMyStructArray ==&(xMyStructArray [0])

C/C++ creates an array in contiguous memory cells, and when you use the [] operator, you tell it to access to the respective cell of that array, starting from the pointer address corresponding to first array element address. C / C ++在连续的内存单元中创建一个数组,当您使用[]运算符时,您告诉它从对应于第一个数组元素地址的指针地址开始访问该数组的相应单元。 So it knows the size of your type, and go to the right address. 因此它知道您的类型大小,然后转到正确的地址。

The pointer arithmetic works the same. 指针算法的工作原理相同。 When you sum 1 to a pointer, the compiler checks the size of the type corresponding to that pointer, and then goes to the next position in memory compatible with that type. 当您对一个指针求和1时,编译器将检查与该指针对应的类型的大小,然后转到与该类型兼容的内存中的下一个位置。

Both are same, Compiler will treat iptr[3] and 3[iptr] as *(iptr+3) and *(3+iptr) only. 两者相同,Compiler只会将iptr[3]3[iptr]视为*(iptr+3)*(3+iptr)

int a[5][10] = {0}; Consider this 2 dimentional array. 考虑这个二维数组。 Here a[i][j] means *(*(a+i) + j) , so we can write like i[a][j] also which means *(*(i + a) + j) . 这里a[i][j]表示*(*(a+i) + j) ,所以我们可以像i[a][j]那样写,也意味着*(*(i + a) + j)

But i[j][a] is wrong, because it will be treated as *(*(i + j)+a) which is wrong. 但是i[j][a]是错误的,因为它将被视为*(*(i + j)+a)这是错误的。 *(i+j) will leads to crash(undefined behaviour). *(i+j)会导致崩溃(不确定的行为)。

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

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