Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between {} and {0} as struct initialisers?

Is there any difference between this:

struct something s = {};

And this?

struct something s = {0};

From all I know, both initialise every member to zero.

like image 214
futlib Avatar asked Feb 15 '23 10:02

futlib


1 Answers

struct something s = {}; isn't valid C (unless they added it in C11), but is valid C++. GCC seems to allow it in C programs as an extension (though I don't see it mentioned in the docs on http://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html, but I might just be missing it).

In C++ it will result in 'value-initialization', which basically means that the default constructor is called for each member (zero-initialization for non-class members).

like image 57
Michael Burr Avatar answered Mar 12 '23 01:03

Michael Burr