futlib
futlib

Reputation: 8408

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.

Upvotes: 4

Views: 577

Answers (1)

Michael Burr
Michael Burr

Reputation: 340188

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).

Upvotes: 8

Related Questions