混乱的数据类型的二维数组 [英] Confusion in data types in a 2D array
问题描述
下面是一个code哪个打印随后加入1.虽然所有的4个基本地址的二维数组的第一个元素的地址是相同的,但他们除了与1显然是不给予应有的相同的结果他们不同的类型。我可以计算出对于某些类型(如果它们是正确的),而不是对所有的。
INT的main()
{
int数组[4] [3];
的printf(%阵U \\ N,数组);类型为int(*)的// [3]
的printf(数组+ 1%U \\ N阵列+ 1);
的printf(&安培;阵列%U \\ N,&安培;数组); // .... ???
的printf(&安培;阵+ 1%U \\ N,&安培;阵列+ 1);
的printf(数组[0]%U \\ N阵列[0]); // int *类型
的printf(数组[0] +1%U \\ N阵列[0] +1);
的printf(&安培;数组[0]%U \\ N,&安培;数组[0]); // .... ???
的printf(&安培;数组[0] +1%U \\ N,&安培;数组[0] +1);
}
你能以理解加1以下的gcc机样本输出给定为快速参考后,所涉及的指针运算解释详细地对每个基址的类型。
阵列3214383040
阵列+ 1 3214383052
&安培;阵3214383040
&安培;阵列+ 1 3214383088
数组[0] 3214383040
数组[0] +1 3214383044
&安培;数组[0] 3214383040
&安培;数组[0] +1 3214383052
有 int数组[4] [3];
以下适用
阵列
是4个元素的数组。每个元素都是用3 INT
秒的数组。在本身使用名称大多数情况下,使该阵列衰变到一个指针到它的第一元件;那么阵列
成为一个指向数组3 INT
取值
阵列+ 1
是一个指向3整型数组。在这里,阵列
衰减到指针和 1
指3阵列 INT
&放大器;阵列
是整个数组的地址。它指向4阵列3的整数类型的数组对象
&放大器;阵列+ 1
是2(这确实不存在)一个伪元素 - 4阵列的阵列的数组3整数
数组[0]
是3个整数的数组。它通常衰变的指针的第一个元素
数组[0] + 1
指向数组2号
INT
[0 ]
&放大器;数组[0]
的3个整数类型的数组对象的地址
&放大器;数组[0] +1
3整型数组的数组的第二个元素
的 PS。我会尽力做午饭后绘图(ASCII)。的
的嗯......图纸是艰难的:)的
在努力,我想我可以做一个更好的图形。
这是我能想出...
int数组[4] [3] ........ [aaabbbcccddd] ........
其中aaa,BBB,CCC,DDD是3个整数'阵列 按[]重present对象本身;在{}重新present指针。 阵列(对象)........ [AAABBBCCCDDD] ........ INT [4] [3]
阵列(腐烂)==> ........ {AAA} bbbcccddd ........ INT(*)[3] 阵列+1 ==> ........ AAA {BBB} cccddd ........ INT(*)[3] &阵列==> ........ {aaabbbcccddd} ........ INT(*)[4] [3]
&阵列+1 ==> ........ aaabbbcccddd {XXXXXXXXXXXX} ........ INT(*)[4] [3] 阵列[0](对象)........ [AAA] bbbcccddd ........ INT [3]
数组[0](蛀牙)==> ........ {A} aabbbcccddd ........为int * 数组[0] + 1 ==> ........一个{A} abbbcccddd ........为int * 与阵列[0] ==> {........} AAA ........ bbbcccddd INT(*)[3]
与阵列[0] + 1 ==> ........ AAA {} BBB ........ cccddd INT(*)[3]
Here is a code which print the address of the first element of an 2D array followed by an addition of 1. Though all 4 base addresses are same but their addition with 1 is obviously not giving the same result due the their different "types". I can figure out the type for some(in case they are correct) but not for all.
int main()
{
int array[4][3];
printf("array %u\n",array); //of type int(*)[3]
printf("array+1 %u\n",array+1);
printf("&array %u\n",&array); //....???
printf("&array+1 %u\n",&array+1);
printf("array[0] %u\n",array[0]); //of type int*
printf("array[0]+1 %u\n",array[0]+1);
printf("&array[0] %u\n",&array[0]); //....???
printf("&array[0]+1 %u\n",&array[0]+1);
}
Can you explain the "type" of each base address in detail in order to understand the pointer arithmetic involved after adding 1. A sample output for gcc machine is given below for quick reference.
array 3214383040
array+1 3214383052
&array 3214383040
&array+1 3214383088
array[0] 3214383040
array[0]+1 3214383044
&array[0] 3214383040
&array[0]+1 3214383052
Having int array[4][3];
the following applies
array
is an array with 4 elements. Each element is an array with 3 int
s. In most circumstances using the name by itself makes the array decay to a pointer to its first element; then array
becomes a pointer to arrays of 3 int
s
array + 1
is a pointer to an array of 3 ints. Here array
decays to pointer and the 1
refers to array of 3 int
&array
is the address of the whole array. It points to objects of type array of 4 arrays of 3 ints
&array + 1
is the 2nd (which really does not exist) element of a pseudo-array of arrays of 4 arrays of 3 ints
array[0]
is an array of 3 ints. It usually decays to a pointer to the first element
array[0] + 1
points to the 2nd int
in array[0]
&array[0]
address of an object of type array of 3 ints
&array[0]+1
2nd element of an array of arrays of 3 ints
PS. I'll try to make a drawing (ASCII) after lunch.
Hmmm ... drawing is tough :)
Before trying, I thought I could make a better drawing. This is the best I could come up with ...
int array[4][3] ........[aaabbbcccddd]........ where aaa, bbb, ccc, ddd are arrays of 3 ints' the [] represent the object itself; the {} represent pointers. array (object) ........[AAABBBCCCDDD]........ int[4][3] array (decayed) ==> ........{aaa}bbbcccddd........ int(*)[3] array + 1 ==> ........aaa{bbb}cccddd........ int(*)[3] &array ==> ........{aaabbbcccddd}........ int(*)[4][3] &array + 1 ==> ........aaabbbcccddd{xxxxxxxxxxxx}........ int(*)[4][3] array[0] (object) ........[AAA]bbbcccddd........ int[3] array[0] (decayed) ==> ........{a}aabbbcccddd........ int* array[0] + 1 ==> ........a{a}abbbcccddd........ int* &array[0] ==> ........{aaa}bbbcccddd........ int(*)[3] &array[0] + 1 ==> ........aaa{bbb}cccddd........ int(*)[3]
这篇关于混乱的数据类型的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!