如果我有一个整数指针数组,并且我想访问数组中的一个指针所指向的整数的值,该怎么做?
例子:
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;
如何通过数组 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?
有一些可能的变体,但最清晰和最传统的方法可能是使用索引运算符 []
来选择所需的数组元素(指针)和取消引用运算符, *
,根据结果获取指向的int。索引运算符具有更高的优先级(它属于单个最高优先级运算符组),但如果您不记得它并且由于某种原因无法查找它,那么您始终可以使用括号来确保所需的顺序的评价。
示例:
int *array[2];
// ... assign valid pointer values to array elements ...
int x = *array[1];
这与将任何其他类型的数组元素用作任何其他运算符的操作数的方式没有特别不同。
关于c - 如何访问指针数组中的指针指向的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52306097/