对于这个问题的未来读者,EricPostischil的回答是正确的,我接受了它,因为它是正确的。下面是一个示例程序和输出,演示应用于多维数组的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个字节的ints 0x0101010101==16843009。