C中的数组初始化 [英] Array initialisation in C

查看:155
本文介绍了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 in c?

I know array1 is actually an int and array2 and array3 are arrays, so why doesn't a c 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆