Initialize array in c in the format `int a[3]={0,};` [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 03:22:03

问题


I had seen a source where array initialize in int arr[3] ={0,}; What does it mean ? I normally use this format int arr[3]={0}; can i know what is the difference


回答1:


This is probably a dup, but I'll take a shot anyhow.

There is no difference between int arr[3] ={0,}; and int arr[3] ={0};.

Ref: C11 6.7.9:

    initializer:
         assignment-expression
         { initializer-list }
         { initializer-list , }

Both forms of initializer lists are considered initializers. The form with the comma at the end is preferred by many because it makes it easier to rearrange or add elements to the list during code maintenance.

 int arr[3] ={0,}; 

declares an array of three elements and initializes the first element to 0. When you do a partial initialization, the rest of the array is automatically initialized with zeros.

Ref. C11 6.7.9:

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.



来源:https://stackoverflow.com/questions/38471904/initialize-array-in-c-in-the-format-int-a3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!