gpt4 book ai didi

c++ - 二维数组定义

转载 作者:太空宇宙 更新时间:2023-11-04 16:07:26 25 4
gpt4 key购买 nike

根据我的理解,当我们定义一个数组时,我们需要指定它的大小

显式地让编译器知道数组需要占用的内存。

因此,

案例一:

int a[][3]; //illegal as we will not know the size of the int array
a[0][1]=2;
a[1][3]=4;

但是当我添加这样的东西时:

案例二:

int a[][3]={0}; //the compiler didn't complain at all
a[0][1]=2;
a[1][3]=4;

为什么编译器在这种情况下不显示任何错误消息?案例 2 中的定义只告诉编译器有多少列,但行数仍然未知?编译器不应该以与案例 1 相同的方式行事吗?有什么理由可以解释这个吗?非常感谢

最佳答案

当你将数组定义为

int a[][3]={0};

编译器将值赋给上面声明的数组:

int a[1][3]

所以它没有提示

类似地,如果你这样做:

int a[][3]={0,2};

需要:

整数[2][3]

关于c++ - 二维数组定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33131007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com