Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unspecified dimension of array initialisation with {}

I am wondering if:

int a[] = {1, 2};

allocates sizeof(int) * number of constants inside brackets

int a[5] = {1, 2};

assigns constants to array fields from 0 to 1 and then fills with 0

int a[5] = {};

fills with 0

What happens when I do:

int a[] = {};

Thanks.

like image 869
Dejan Pekter Avatar asked May 17 '26 15:05

Dejan Pekter


1 Answers

int a[5] = {};

and

 int a[] = {};

are not valid C definitions.

In GNU C (C with gcc extensions), you can use empty {} and it is considered the same as {0}.

Note that int [] is a incomplete type. When initializing an array of an incomplete type with explicit initializers, the type is completed and the number of elements of the array is then the number of elements in the brace enclosed initializer list.

So int a[] = {0}; defines an array of 1 element in C and in GNU C int a[] = {}; does the same.

like image 58
ouah Avatar answered May 20 '26 07:05

ouah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!