Should I use {} or {0} to initialize an array / structure?

I have a faint memory that code like int x[4] = {};that used to initialize struct / array values ​​by default is based on the non-standard (but widespread) extension that first appeared in gcc, and the correct version (as obviously indicated in the standard)int x[4] = { 0 };

Is it correct?

+4
source share
1 answer

In C, the list of initializers is defined as follows:

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

In C ++, it is defined as follows

braced-init-list:
{ initializer-list ,opt }
{ }

As you can see, C ++ allows an empty list of element lists, while the list of C initializers cannot be omitted.

So you can write in C ++

int x[4] = {}; 

C ( , ).

int x[4] = { 0 };

.

+6

All Articles