C 为什么此程序打印0而不是指定数组中的特定值集?

C 为什么此程序打印0而不是指定数组中的特定值集?,c,arrays,multidimensional-array,initialization,initializer-list,C,Arrays,Multidimensional Array,Initialization,Initializer List,我想知道为什么这个小程序中的printf函数返回0而不是数字数组3 2: int main(){ int mat[2][2][2] = {{3,1,1},{2,2,2}}; printf("first x, 2nd y, 2nd z = %d\n",mat[0][1][1]); } 在C中使用X×Y矩阵检索任何值XxY都是轻而易举的事,但一旦我添加了另一个维度,我就遇到了这个问题。我想我一定是误解了C处理数组坐标的方式。非常感谢 此代码将告诉您为什么结果为0: int mai

我想知道为什么这个小程序中的
printf
函数返回
0
而不是数字数组
3 2

int main(){
    int mat[2][2][2] = {{3,1,1},{2,2,2}};

    printf("first x, 2nd y, 2nd z = %d\n",mat[0][1][1]);
}

在C中使用X×Y矩阵检索任何值XxY都是轻而易举的事,但一旦我添加了另一个维度,我就遇到了这个问题。我想我一定是误解了C处理数组坐标的方式。非常感谢

此代码将告诉您为什么结果为
0

int main(){
    int mat[2][2][2] = {{3,1,1},{2,2,2}};

    for(int i = 0; i < 2; i++)
         for(int j = 0; j < 2; j++)
             for(int k = 0; k < 2; k++)
                 printf("mat[%d][%d][%d] = %d \n", i, j, k, mat[i][j][k]);
   return 0;
}
当您声明:

int mat[2][2][2] = {{3,1,1},{2,2,2}};

这意味着您在程序中撒谎,它是2D数组,而不是3D数组。

此代码将告诉您为什么结果是
0

int main(){
    int mat[2][2][2] = {{3,1,1},{2,2,2}};

    for(int i = 0; i < 2; i++)
         for(int j = 0; j < 2; j++)
             for(int k = 0; k < 2; k++)
                 printf("mat[%d][%d][%d] = %d \n", i, j, k, mat[i][j][k]);
   return 0;
}
当您声明:

int mat[2][2][2] = {{3,1,1},{2,2,2}};
这意味着您在程序中撒谎,它是2D数组,而不是3D数组。

In

int mat[2][2][2]={{3,1,1},{2,2,2}

您声明了一个三维数组,但对二维数组进行了初始化,这些值未放置在您期望的位置

#include <stdio.h>

int main(){
  int mat[2][2][2] = {{3,1,1},{2,2,2}};

  for (int i = 0; i != 2; ++i)
    for (int j = 0; j != 2; ++j)
      for (int k = 0; k != 2; ++k)
        printf("%d %d %d -> %d\n", i, j, k, mat[i][j][k]);
  return 0;
}
此外,由于数组的int值为8,但init值只有6,因此编译器会在中用0初始化两个未指定的条目

int mat[2][2][2]={{3,1,1},{2,2,2}

您声明了一个三维数组,但对二维数组进行了初始化,这些值未放置在您期望的位置

#include <stdio.h>

int main(){
  int mat[2][2][2] = {{3,1,1},{2,2,2}};

  for (int i = 0; i != 2; ++i)
    for (int j = 0; j != 2; ++j)
      for (int k = 0; k != 2; ++k)
        printf("%d %d %d -> %d\n", i, j, k, mat[i][j][k]);
  return 0;
}

此外,由于数组有8个int,但init值只有6,因此编译器使用0初始化两个未指定的条目。您有一个三维数组,但您正在使用1个额外值初始化一个2级数组。考虑一下:

#include <cstdio>

int main(){
    int mat[2][2][2] = {
       {{3, 3}, {1, 1}},
       {{1, 1}, {1, 1}}
    };

    printf("first x, 2nd y, 2nd z = %d\n", mat[0][1][1]);
}
#包括
int main(){
国际材料[2][2][2]={
{{3, 3}, {1, 1}},
{{1, 1}, {1, 1}}
};
printf(“第一个x,第二个y,第二个z=%d\n”,mat[0][1][1]);
}

您有一个三维数组,但您正在用一个额外值初始化一个2级数组。考虑一下:

#include <cstdio>

int main(){
    int mat[2][2][2] = {
       {{3, 3}, {1, 1}},
       {{1, 1}, {1, 1}}
    };

    printf("first x, 2nd y, 2nd z = %d\n", mat[0][1][1]);
}
#包括
int main(){
国际材料[2][2][2]={
{{3, 3}, {1, 1}},
{{1, 1}, {1, 1}}
};
printf(“第一个x,第二个y,第二个z=%d\n”,mat[0][1][1]);
}

您指定了三维数组,但只指定了二维数组。此外,每个元素应该有两个条目,但您有三个条目

如果您应该看到如下警告:

test.c:4:26: warning: suggest braces around initialization of subobject [-Wmissing-braces]
    int mat[2][2][2] = {{3,1,5},{2,2,2}};
...more like that...
test.c:6:44: warning: format specifies type 'int' but the argument has type 'int *' [-Wformat]
    printf("z for first coordinate = %d\n",mat[0][2]);
                                     ~~    ^~~~~~~~~
test.c:6:44: warning: array index 2 is past the end of the array (which contains 2 elements)
      [-Warray-bounds]
    printf("z for first coordinate = %d\n",mat[0][2]);
                                           ^      ~
test.c:4:5: note: array 'mat' declared here
    int mat[2][2][2] = {{3,1,5},{2,2,2}};
    ^
int mat[2][2][2]
将按如下方式初始化

int mat[2][2][2] = {
    {
        {3,1}, {1,1},
    },
    {
        {1,1}, {2,2},
    }
};
您将永远无法从
mat[0][1][1]
获得
3 2
。它将只返回一个值。在这种情况下,1


如果要存储2个三维坐标的列表,请使用[2][3]

int mat[2][3] = {{3,1,1},{2,2,2}};
问第一个x和第二个y的z是什么没有意义。这就像问新泽西州丹佛的海拔高度(丹佛在科罗拉多州)。第一个x和第二个y是两个不同坐标的一部分,具有不同的z

相反,可以得到第一个坐标的z,如下所示

printf("z for first coordinate = %d\n",mat[0][2]);

第一个坐标是
mat[0]
,其z是第三个属性,即第二个索引
mat[0][2]

您指定了一个三维数组,但只指定了二维数组。此外,每个元素应该有两个条目,但您有三个条目

如果您应该看到如下警告:

test.c:4:26: warning: suggest braces around initialization of subobject [-Wmissing-braces]
    int mat[2][2][2] = {{3,1,5},{2,2,2}};
...more like that...
test.c:6:44: warning: format specifies type 'int' but the argument has type 'int *' [-Wformat]
    printf("z for first coordinate = %d\n",mat[0][2]);
                                     ~~    ^~~~~~~~~
test.c:6:44: warning: array index 2 is past the end of the array (which contains 2 elements)
      [-Warray-bounds]
    printf("z for first coordinate = %d\n",mat[0][2]);
                                           ^      ~
test.c:4:5: note: array 'mat' declared here
    int mat[2][2][2] = {{3,1,5},{2,2,2}};
    ^
int mat[2][2][2]
将按如下方式初始化

int mat[2][2][2] = {
    {
        {3,1}, {1,1},
    },
    {
        {1,1}, {2,2},
    }
};
您将永远无法从
mat[0][1][1]
获得
3 2
。它将只返回一个值。在这种情况下,1


如果要存储2个三维坐标的列表,请使用[2][3]

int mat[2][3] = {{3,1,1},{2,2,2}};
问第一个x和第二个y的z是什么没有意义。这就像问新泽西州丹佛的海拔高度(丹佛在科罗拉多州)。第一个x和第二个y是两个不同坐标的一部分,具有不同的z

相反,可以得到第一个坐标的z,如下所示

printf("z for first coordinate = %d\n",mat[0][2]);

第一个坐标是
mat[0]
,其z是第三个属性,即第二个索引
mat[0][2]

问题在于此数组元素
mat[0][1][1]
没有显式初始值设定项。所以它是零初始化的

那么你有这样的声明吗

int mat[2][2][2] = {{3,1,1},{2,2,2}};
然后,作为聚合的数组mat[0]的第一个元素由该列表{3,1,1}初始化,数组mat[1]的第二个元素由该列表{2,2,2}初始化

对于元素mat[0]的元素,如果没有指定大括号,那么mat[0]的元素将按如下顺序初始化

mat[0][0][0] = 3
mat[0][0][1] = 1
mat[0][1][0] = 1 
元素(数组)mat[0]的所有其他元素都已初始化为零

这是一个演示程序

#include <stdio.h>

int main(void) 
{
    int a[2][2][2] = {{3,1,1},{2,2,2}};

    printf( "%d, %d, %d\n", a[0][0][0], a[0][0][1], a[0][1][0] );
    printf( "%d, %d, %d\n", a[1][0][0], a[1][0][1], a[1][1][0] );

    return 0;
}

问题是此数组元素
mat[0][1][1]
没有显式的初始值设定项。所以它是零初始化的

那么你有这样的声明吗

int mat[2][2][2] = {{3,1,1},{2,2,2}};
然后,作为聚合的数组mat[0]的第一个元素由该列表{3,1,1}初始化,数组mat[1]的第二个元素由该列表{2,2,2}初始化

对于元素mat[0]的元素,如果没有指定大括号,那么mat[0]的元素将按如下顺序初始化

mat[0][0][0] = 3
mat[0][0][1] = 1
mat[0][1][0] = 1 
元素(数组)mat[0]的所有其他元素都已初始化为零

这是一个演示程序

#include <stdio.h>

int main(void) 
{
    int a[2][2][2] = {{3,1,1},{2,2,2}};

    printf( "%d, %d, %d\n", a[0][0][0], a[0][0][1], a[0][1][0] );
    printf( "%d, %d, %d\n", a[1][0][0], a[1][0][1], a[1][1][0] );

    return 0;
}

<代码> {{3,1,1},{2,2,2} }/Cyp>你为2D数组初始化,不是3DIT的C++,但它是相同的原理。<代码> {{3,1,1},{2,2 } } < /C> >你为2D数组初始化,而不是3DIT的C++,但它是相同的原理。