Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: How does nested braces for array of struct initialization work?

Tags:

c

struct mystruct s[10] = {{0}};

This appears to initialize an array of structs to 0. How does the nested braces syntax work?

like image 539
zer0stimulus Avatar asked Oct 07 '10 16:10

zer0stimulus


Video Answer


1 Answers

Any fields not specified are initialized to zero. So here you have an array of structs. You're initializing the first element of the array with a structure initializer that initializes the first element of the structure to zero. The rest of the first structure and the rest of the array elements will all be zero too. It's a nice idiom.

like image 71
xscott Avatar answered Sep 19 '22 13:09

xscott