对于这个问题的未来读者,Eric Postpischil 的回答是正确的,我接受了它,因为它是。这是一个示例程序和输出,用于演示 sizeof 运算符/函数应用于多维数组。
一般来说,如果声明为整数数组
int mda[x][y][z];
then mda[a][b][c] == *(mda + ((a *( y* z)) + (b*z) + (c))
重要的是,至少对于我最初提出的问题而言,索引到具有比程序声明的“维度”更少的数组仍然会编译和维护下一个数组的“sizeof”。
mda[a] == *(mda + (a * (y*z))) AND sizeof(mda[a]) == sizeof(array_type) * y * z
mda[a][b] == *(mda + (a * (y*z)) + (b * z)) AND sizeof(mda[a][b]) == sizeof(array_type) * z
好的,这里有一个示例程序,您可以在在线 IDE 中运行并验证:
#include <stdio.h>
#define X (4)
#define Y (10)
#define Z (5)
int multi_dimension_array[X][Y][Z];
void print_array(){
for(int i=0; i<X; i++){
printf("THIS IS THE %dth value of X\n", i);
for(int j=0; j<Y; j++){
for(int k=0; k<Z; k++){
printf("%d ", multi_dimension_array[i][j][k]);
}
printf("\n");
}
}
}
int main(void) {
printf("%d %d %d %d\n", sizeof(multi_dimension_array[0][0][0]), sizeof(multi_dimension_array[0][0]), sizeof(multi_dimension_array[0]), sizeof(multi_dimension_array));
memset(multi_dimension_array,0x01,sizeof(multi_dimension_array));
print_array();
memset(&multi_dimension_array[2],0x0,sizeof(multi_dimension_array[2]));
printf("\n\n\nNEXT MEMSET with the X=2 zeroed out\n\n\n");
print_array();
return 0;
}
这是这个程序的输出:
4 20 200 800
THIS IS THE 0th value of X
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
THIS IS THE 1th value of X
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
THIS IS THE 2th value of X
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
THIS IS THE 3th value of X
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
NEXT MEMSET with the X=2 zeroed out
THIS IS THE 0th value of X
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
THIS IS THE 1th value of X
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
THIS IS THE 2th value of X
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
THIS IS THE 3th value of X
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
*请注意,memset 将每个字节设置为第二个参数的值,在本例中为 0x01,这使得 4 字节整数为 0x01010101 == 16843009。