r *_* l -4 c arrays pointers multidimensional-array
int a[3][4] = {
1,2,3,4,
5,6,7,8,
9,10,11,12,
};
printf("%u %u %u \n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
Run Code Online (Sandbox Code Playgroud)
Joh*_*ode 12
C语言中数组速成课程的时间
首先,让我们修复数组的初始值设定项:
int a[3][4] = {
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12}
};
Run Code Online (Sandbox Code Playgroud)
这定义了一个由4个元素组成的3元素数组int.表达式 的类型a是"4元素阵列的3元素阵列int".
现在为引起头痛的部分.除非它是sizeof或一元运算&符的操作数,或者它是一个字符串文字用于初始化声明中的另一个数组,数组类型的表达式将其类型隐式转换("衰减")为指针类型.
如果表达式a本身出现在代码中(例如在类似的语句中printf("%p", a);,其类型从"4元素数组的4元素数组int"转换为"指向4元素数组的数组int",或者int (*)[4].)同样,如果表达a[i]出现在代码,它的类型是从"的4个元素的数组转换int(" int [4]),以"指针int"(),int *如果a或者a[i]是任操作数sizeof或&,然而,转换不会发生.
类似地,数组下标是通过指针算法完成的:表达式a[i]被解释为它被写入*(a+i).您i从数组的基数偏移元素并取消引用结果.因此,a[0]是相同的*(a + 0),它是相同的*a. a[i][j]和写作一样*(*(a + i) + j).
这是一个总结以上所有内容的表格:
Expression Type Decays To Resulting Value
---------- ---- --------- -----
a int [3][4] int (*)[4] Address of the first element of the array
&a int (*)[3][4] n/a Same as above, but type is different
*a int [4] int * Same as above, but type is different
a[0] int [4] int * Same as above
*(a+0) int [4] int * Same as above
a[i] int [4] int * Address of the first element of the i'th subarray
*(a+i) int [4] int * Same as above
&a[i] int (*)[4] n/a Same as above, but type is different
*a[i] int n/a Value of the 0'th element of the i'th subarray
a[i][j] int Value of the j'th element of the i'th subarray
*(a[i]+j) int Same as above
*(*(a+i)+j) int Same as above
希望这应该为您提供所需的一切,以确定输出应该是什么.但是,printf声明应写成
printf("%p %d %d\n", (void *) a[0]+1, *(a[0]+1), *(*(a+0)+1));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1064 次 |
| 最近记录: |