"struct a a1 = {0};" different from "struct a a2 = {5};" What for?

If struct a a1 = {0}; initializes all elements (of different types) of the structure to zero, then struct a a2 = {5}; should initialize it to 5 .. no?

 #include <stdio.h> typedef struct _a { int i; int j; int k; }a; int main(void) { a a0; a a1 = {0}; a a2 = {5}; printf("a0.i = %d \n", a0.i); printf("a0.j = %d \n", a0.j); printf("a0.k = %d \n", a0.k); printf("a1.i = %d \n", a1.i); printf("a1.j = %d \n", a1.j); printf("a1.k = %d \n", a1.k); printf("a2.i = %d \n", a2.i); printf("a2.j = %d \n", a2.j); printf("a2.k = %d \n", a2.k); return 0; } 

An uninitialized structure contains garbage values

 a0.i = 134513937 a0.j = 134513456 a0.k = 0 

Initialized structure 0 contains all elements initialized to 0

 a1.i = 0 a1.j = 0 a1.k = 0 

The initialized structure 5 contains only the first element initialized to 5 , and the remaining elements are initialized to 0 .

 a2.i = 5 a2.j = 0 a2.k = 0 

If a2.j and a2.k will always be initialized to 0 during a a2 = {5}; (or), this is undefined behavior

OTOH, why I do not see all s2 elements initialized to 5 . How is struct initialization performed during {0} and how does it differ when using {5} ?

+4
source share
6 answers

Link:

C99 Standard 6.7.8.21

If the list enclosed in brackets contains fewer initializers than the element or elements of the collection or fewer characters in the string literal used to initialize an array with a known size than in the array, the rest of the aggregate must be initialized implicitly in the same way as objects, having a static storage duration.

[EDIT]

Static objects and implicit initialization:

The storage duration of an object determines the lifetime of the object.
There are 3 storage periods:
static, automatic and dedicated

declared outside of all blocks, and those explicitly declared using the static storage class specifier have a static storage duration . Static variables are initialized to zero by default by the compiler.

Consider the following program:

 #include<stdio.h> int main() { int i; static int j; printf("i = [%d]",i); printf("j = [%d]",j); return 0; } 

In the above program, i has automatic storage, and since it is not explicitly initialized, its value is Undefined .
So far, j has a static storage duration and is guaranteed to be initialized by compiler 0 .

+10
source

Missing values ​​will always be initialized to zero, as the standard says so. So you essentially

 struct a a1 = { 0, 0, 0 }; 

and

 struct a a2 = { 5, 0, 0 }; 

which of course is different.

+8
source

No. In C, if your initialization list is incomplete, all missing indexes will be filled with 0. So this is:

 int a[3] = {0}; int b[3] = {5}; 

effectively becomes:

 int a[3] = {0, 0, 0}; int b[3] = {5, 0, 0}; 

This is why it works with {0} but does not work with {5} .

+2
source

This does not work for

 struct x { int *y; /* ... */ }; struct x xobj = {5}; 
+2
source

See the designated initializers in the GCC documentation.

+1
source

In both cases, the behavior is the same. If there are fewer initializers than elements in the aggregate, the rest of the elements are initialized as if they were declared static , i.e. they will be initialized to 0 or NULL.

It is just that in the first case, the explicit initializer has the same meaning as the implicit initializer.

If you want to initialize all the elements of your aggregate with something other than 0, you will need to provide an explicit initializer for each of them, that is:

 a a2 = {5, 5, 5}; 
+1
source

Source: https://habr.com/ru/post/1389502/


All Articles