[英]cannot understand addressing in 2d array in c
main()
{
int a[3][2] = { {1,2},{3,4},{5,6}};
for(int i=0;i<3;i++)
for(int j=0;j<2;j++)
{
printf("%d", a[i][j]);
printf("\t %d\n", &a[i][j]);
}
printf("\n%d", *(a+1));
printf("\n%d", *a+1);
}
the output of *(a+1) is different from *a+1. *(a + 1)的输出与* a + 1不同。
*(a+1) is pointing to 3 rd element whereas *(a + 1)指向第3个元素而
*a+1 is outputting the 2nd value * a + 1输出第二个值
the output of *(a+1) is different from *a+1. *(a + 1)的输出与* a + 1不同。
Yes, due to operator precedence . 是的,由于运营商的优先权 。 *a + 1
means... *a + 1
表示......
Dereference
a
(which returns anint
) and add1
to it. 取消引用a
(返回一个int
)并向其添加1
。 Return the result (2
) 返回结果(2
)
However, *(a + 1)
says... 但是, *(a + 1)
说......
Add
1
to the pointera
and dereference it, ie, get the value at the addressa + sizeof(int[2])
. 将1
添加到指针a
并取消引用它,即获取地址a + sizeof(int[2])
。
The "value* happens to be the first element of the second array. Remember; adding n to a pointer type advances the address by n elements . In this case, each element is an array of int
with two elements of its own. “值*恰好是第二个数组的第一个元素。记住;在指针类型中添加n会使地址前进n个元素 。在这种情况下,每个元素都是一个int
数组,其中包含两个自己的元素。
That should answer the next two questions as well. 那也应该回答接下来的两个问题。 After reading up on operator precedence, start studying pointer arithmetic . 在读取运算符优先级后,开始研究指针运算 。
C doesn't really have multi-dimensional arrays, it has arrays of arrays. C实际上没有多维数组,它有数组数组。 int a[3][2]
declares a 3-element array whose elements are 2-dimensional arrays of ints. int a[3][2]
声明一个3元素数组,其元素是2维的int数组。
Arithmetic on pointers takes this into account, it increments by the size of the objects that the pointer points to. 指针算术会考虑到这一点,它会增加指针所指向的对象的大小。 So (a+1)
evaluates to the second element of a
, which is a pointer to the array {3, 4}
. 所以(a+1)
的计算结果为第二元件a
,这是一个指向数组{3, 4}
Indirecting through this pointer gets you its first element, which is 3. 通过这个指针指向你的第一个元素是3。
*a
on the other hand, indirects through pointer to the first element of a
, which evaluates to 1. Then when you add 1 to this, you get 2. It's not actually returning the 2nd element, it just looks like it because 1+1 = 2. Try changing your initialization to: *a
另一方面,通过指向a
的第一个元素的方向,其值为1.然后当你向它添加1时,你得到2.它实际上并没有返回第二个元素,它只是看起来像是因为1+ 1 = 2.尝试将初始化更改为:
int a[3][2] = { {1,3},{5,7},{9,11}};
and your output will be 5 and 2. 你的输出将是5和2。
You should read about operator precedence. 您应该阅读运算符优先级。 In the second case *a+1
the unary operator *
binds more than the binary operator +
. 在第二种情况下*a+1
,一元运算符*
比二元运算符+
绑定更多。 So, the operation is treated like adding one to the value pointed by a
. 因此,操作被视为添加一个指向a
指向的值。
In the former case *(a+1)
, you are addressing the next element pointed by a
and dereferencing it to get its value. 在前一种情况*(a+1)
,您正在寻找a
指向的下一个元素并对其进行解除引用以获取其值。
HTH. HTH。
Yes,that's correct. 对,那是正确的。 Because a 2D array represent an array of 1D arrays.In this case in a[3][2] = { {1,2},{3,4},{5,6}}
,name of array ,that is a acts as zeroth element of array. 因为2D阵列表示1D arrays.In阵列这种情况下, a[3][2] = { {1,2},{3,4},{5,6}}
阵列的名称,这是一个充当数组的第0个元素。 Since zeroth element of our 2-D array is 1-D array of 2 integers{1,2}.Thus, (a+1)
refers to address of first element of 2D array {3,4}
and *(a+1) thus gives value at that address ie 3. While,*a gives access to first element and than it increments it by one giving 2. 由于我们的二维数组的第零个元素是2个整数{1,2}的1-D数组。因此, (a+1)
指的是2D数组{3,4}
的第一个元素的地址和*(a + 1)因此,在该地址给出值即3.然而,* a提供对第一个元素的访问,而不是将其递增1给出2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.