C中的数组初始化 [英] Array initialisation in C
问题描述
我就怀疑下面这段code的:
INT的main()
{
INT数组1 = {1,2,3,4,5}; //在C ++的错误,在C警告
INT数组2 [] = {1,2,3,4,5};
INT ARRAY3 [5] = {1,2,3,4,5};
}
这件code的给出了3行误差 C ++
,但不是在 C
<? / p>
我知道数组1
实际上是一个 INT
和数组2
和 ARRAY3
是数组,那么为什么不一个 C
编译器显示一个错误,但只是一个警告:在标量初始化多余的元素
有没有使用这样的定义的,为什么它很有效的 C
?
这是无效C.见C11 6.7.9:
没有初始化将试图以提供一个对象的值不
其中所包含的实体被初始化
块引用>我猜你正在使用gcc。然后,如果你希望你的程序表现为严格的标准C,编译这样:
的gcc -std = C11 -pedantic-错误
给
错误:标量初始化多余的元素
块引用>I have doubt regarding the following piece of code :
int main() { int array1 = {1,2,3,4,5}; //error in c++ , warning in c int array2[] = {1,2,3,4,5}; int array3[5] = {1,2,3,4,5}; }
This piece of code gives a error on line 3 in
c++
but not inc
?I know
array1
is actually anint
andarray2
andarray3
are arrays, so why doesn't ac
compiler show a error , but just a warning: "excess elements in scalar initialization"Is there a use of such a definition and why is it valid in
c
?解决方案It is not valid C. See C11 6.7.9:
No initializer shall attempt to provide a value for an object not contained within the entity being initialized.
I would guess that you are using gcc. Then if you want your program to behave as strict standard C, compile it as such:
gcc -std=c11 -pedantic-errors
gives
error: excess elements in scalar initializer
这篇关于C中的数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!