在下面的代码中:
int main()
{
int i = 15, j = 1;
int *a[] = {&i, &j};
printf("%d", (*a)[0]);
return 0;
}
(*a)[0]
的输出是 15
(i 的值)但是当我尝试检查 (*a)[1]
它给出了一个垃圾值。我希望相同的表达式适用于数组中的所有条目,但它仅适用于数组中的第一个元素。
最佳答案
在这里你已经声明了指针数组。首先你应该知道数组的各种表示。 a[i] 是怎么计算的? a 给你数组的基址。
a[ i ] = *(a+i)
(*a)[0] = *(*a+0)
(*a)[1] = *(*a+1)
Thus the value of (*a)[0] = *(address of i + 0) = Value of i
And the value of (*a)[1] = *(address of i + 1) = value at the location of (add of i + 2) i.e. a garbage value.
在上面的问题中,如果使用 *( a[ 0 ] ) 和 *( a[1] ) 就可以正确获取值。
关于c - 试图理解数组符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21480850/