我的老师给了我以下C
示例:
#include<stdio.h>
int main(){
int array[2][2][3]={0,1,2,3,4,5,6,7,8,9,10,11};
printf("%d",array[1][0][2]);
return 0;
}
以及问题:将打印出哪个号码? 可能的答案:
8 似乎是正确答案,但我不理解老师的解释:
array [1] [0] [2]表示: 1 *(2 * 3)+ 0 *(3)+ 3 = 9 - 从零开始的数组的第9个元素,即的 8
答案 0 :(得分:3)
您的数学老师应该先解释一下。对于1D阵列
int 1d_arr[] = {1,2,3,4,5};
1d_arr[2]
等同于*(1d_arr + 2)
,即获取数组1d_arr
的元素,该元素距基本地址2 * sizeof(1d_arr[2])
= 8
个字节或8/4
1}} th = 2 nd 元素,即3
。请注意,基址是1d_arr
本身(数组到指针转换)。
对于2D数组
int 2d_arr[2][3] = { {0,1,2}
{3,4,5}
};
2d_arr[0][2]
等同于*(*(2d_arr + 0) + 2)
,即获取数组2d_arr
的元素,该元素距基本地址0*sizeof(2d_arr[0]) + 2*sizeof(2d_arr[0][2])
= 8
个字节或0/4 + 8/4
数组2d_arr[0]
的1}} th = 2 nd 元素,即2
。
对于3d数组
int 3d_arr[2][2][3] = { {0,1,2}, {3,4,5}
{6,7,8}, {9,10,11}
};
3d_arr[1][0][2]
相当于*(*(*(3d_arr + 1) + 0) + 2)
,即获取数组3d_arr
的元素,其1*sizeof(3d_arr[1]) + 0*sizeof(3d_arr[1][0]) + 2*sizeof(3d_arr[1][0][2])
= 1*(2*3*4) + 0*(3*4) + 2*4
= 32
个字节远离数组24/4 + 0/4 + 8/4
的0 th 元素(数组)的基址或6 + 0 + 2
= 3d_arr[1]
= 2 nd 元素。
答案 1 :(得分:1)
C中的数组只是表示记忆的一种方式。
你有2个,2个和3个值的数组,意思是:
2&#34;最大&#34;包含2&#34; big&#34;的元素元素每个元素包含3个元素。
在内存中它看起来像这样:
1st element in array: array[0][0][0]
2nd element in array: array[0][0][1]
3rd element in array: array[0][0][2]
4th element in array: array[0][1][0]
5th element in array: array[0][1][1]
6th element in array: array[0][1][2]
7th element in array: array[1][0][0]
8th element in array: array[1][0][1]
9th element in array: array[1][0][2]
10th element in array: array[1][1][0]
11th element in array: array[1][1][1]
12th element in array: array[1][1][2]
初始化数组时,它会按顺序匹配元素以确定您的值:0,1,2,3,4,5,6,7,8,9,10,11
如果要打印数组[1] [0] [2],实际上是在数组中打印第9个元素,它已赋值为8。
答案 2 :(得分:1)
本声明:
array [1] [0] [2]表示1 *(2 * 3)+ 0 *(3)+ 3 =从0开始的数组的第9个元素,即8。
是错误的,因为[1] [0] [2]处的元素不是从零开始的第9个元素,而实际上是从0开始的第8个元素,同时,从1开始的第9个元素
因此,正确的陈述应该是:
array [1] [0] [2]表示1 *(2 * 3)+ 0 *(3)+ 2 =从零开始的数组的第8个元素。
或(添加+1以将表单索引转换为编号):
array [1] [0] [2]表示1 *(2 * 3)+ 0 *(3)+ 2 + 1 =从1开始的第9个数组元素。
答案 3 :(得分:1)
以下是数学和输出显示尺寸如何给出结果: 前1秒0第3 2 2 8
Number[FirstDim][SecondDim][ThirdDim] = FirstDim *(Length of Second Dim -1) * (Length of third dim -1) +
SecondDim *(Length of third dim -1) + ThirdDim
这是公式:
#include<stdio.h>
#define FIRST 2
#define SECOND 2
#define THIRD 3
int main(){
int i, j ,k;
int array[FIRST][SECOND][THIRD]={0,1,2,3,4,5,6,7,8,9,10,11};
for (i = 0 ; i < FIRST; i++) {
for (j = 0 ; j < SECOND; j++) {
for (k = 0 ; k < THIRD; k++) {
printf("First %d Second %d Third %d Num %d", i, j, k, array[i][j][k]);
printf("\n");
}
printf("First %d Second %d Third %d \n", i, j, k);
}
printf("First %d Second %d Third %d \n", i, j, k);
}
printf("%d",array[1][0][2]);
return 0;
}
结果:
First 0 Second 0 Third 0 Num 0
First 0 Second 0 Third 1 Num 1
First 0 Second 0 Third 2 Num 2
First 0 Second 1 Third 0 Num 3
First 0 Second 1 Third 1 Num 4
First 0 Second 1 Third 2 Num 5
First 1 Second 0 Third 0 Num 6
First 1 Second 0 Third 1 Num 7
First 1 Second 0 Third 2 Num 8
First 1 Second 1 Third 0 Num 9
First 1 Second 1 Third 1 Num 10
First 1 Second 1 Third 2 Num 11
答案 4 :(得分:0)
int array [2] [2] [3] = {0,1,2,3,4,5,6,7,8,9,10,11};
Consider 3D Array as an "array of 2D Array",like there are 2 * (2 X 3 array)
数组的索引视图
0 -> 0 1 2
3 4 5
1 -> 6 7 8
9 10 11
Real Value in indexes are :
as per indexing table
first value will fall in 1 array of 2D array
(in first row and first column array[0][0][0])
and second in 1 array of 2D array
(in first row and second column array[0][0][1]) and so on...