简体   繁体   English

您如何访问指针数组内部的指针所指向的值?

[英]How do you access the value pointed to by a pointer inside of an array of pointers?

If I have an array of integer pointers, and I want to access the value of the integer pointed to by one of the pointers in the array, how would this be done? 如果我有一个整数指针数组,并且想访问该数组中指针之一所指向的整数值,该怎么办?

Example: 例:

 int *p0, *p1, *p2, *p3, *p4;
 int* index[5] = {p0, p1, p2, p3, p4};
 int variable1 = 1;
 int variable2 = 2;
 int variable3 = 3;
 p0[0] = variable1;
 p0[1] = variable2;
 p0[2] = variable3;

How could I access the value of 123 through the array, index[]? 如何通过数组index []访问123的值?

If I have an array of integer pointers, and I want to access the value of the integer pointed to by one of the pointers in the array, how would this be done? 如果我有一个整数指针数组,并且想访问该数组中指针之一所指向的整数值,该怎么办?

There are some possible variations, but the clearest and most conventional way would probably be to use the indexing operator, [] , to select the desired array element (a pointer) and the dereferencing operator, * , on the result to obtain the pointed-to int. 可能会有一些变化,但最清晰,最传统的方法可能是使用索引运算符[]选择结果所需的数组元素(指针)和解引用运算符* ,以获得有针对性的-诠释。 The indexing operator has the higher precedence (it belongs to the single highest-precedence operator group), but if you don't remember that and are for some reason unable to look it up, then you can always use parentheses to ensure the desired order of evaluation. 索引运算符具有更高的优先级(它属于单个最高优先级运算符组),但是如果您不记得该索引并且由于某种原因无法查找它,则可以始终使用括号来确保所需的顺序评价。

Example: 例:

int *array[2];

// ... assign valid pointer values to array elements ...

int x = *array[1];

This isn't particularly different from how you would use an array element of any other type as an operand of any other operator. 这与将任何其他类型的数组元素用作任何其他运算符的操作数的方式没有特别的不同。

How could I access the value of 123 through the array, index[]? 如何通过数组index []访问123的值?

The first line of your code is unnecessary and the last three lines don't do what you think they are doing. 代码的第一行是不必要的,而后三行则不会执行您认为正在做的事情。

p0[0] = variable1; //*(p0 + 0) = variable1
p0[1] = variable2; //*(p0 + 1) = variable2
p0[2] = variable3; //*(p0 + 2) = variable3

What you are telling your program to do with the first line is to go to the memory location where p0 points and store the value of variable1 there. 您要告诉程序第一行要做的是转到p0指向的存储位置并将variable1的值存储在该位置。 But p0 is an uninitialized pointer so the behavior is undefined. 但是p0是未初始化的指针,因此行为是不确定的。 Then you follow that up with telling your program to go to that same memory address plus an offset and to store something there. 然后,告诉您的程序转到相同的内存地址和偏移量,然后在其中存储内容。 This is dangerous and will likely result in a segmentation fault. 这很危险,可能会导致分段错误。

What you want is something like this: 您想要的是这样的:

int* index[3];

int variable1 = 1;
int variable2 = 2;
int variable3 = 3;

index[0] = &variable1;
index[1] = &variable2;
index[2] = &variable3;

for(int i = 0; i < 3; i++){
    printf("*index[%d] = %d\n", i, *index[i]);
}

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

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