Nan Xiao
Nan Xiao

Reputation: 17477

Initialize the first elememt of the structure or the whole structure?

all:
In C language:

struct A  
{
    int a;
    int b;
};

A aa = {0};  

This statement initialize aa.a only or initialize the whole struture? Or the behavior depends on Compiler?
Thanks in advance!

Upvotes: 5

Views: 253

Answers (4)

John Bode
John Bode

Reputation: 123548

From the standard (N1570)

6.7.9 Initialization

...
10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
— if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
...
21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

So in your example aa.a would be explicitly initialized to 0 due to the initializer, while aa.b would be implicitly initialized to 0 because of the clauses above.

Upvotes: 1

Barath Ravikumar
Barath Ravikumar

Reputation: 5836

This statement initialize aa.a only or initialize the whole struture?

Have a look at the example below , initializing the structure with {3} , is similar to initializing with {3,0}.

Hence in your program when you initialize with {0} , you are actually initializing both a and b (the whole structure) , with {0,0}

#include<stdio.h>

typedef struct A  
{
    int a;
    int b;
}a;

int main()
{
a aa = {3};
printf("\na1 = %d",aa.a);
printf("\nb1 = %d",aa.b);

a bb = {3,0};
printf("\na2 = %d",bb.a);
printf("\nb2 = %d",bb.b);

}

Upvotes: 1

&#181;tex
&#181;tex

Reputation: 908

One more ex:
struct A  
{
    int a;
    int b;
};

struct A aa = {5}; 

This will initialize the whole structure but aa.b will be initialized to 0. If you initialize only few members of a structure then all other members of that will be automatically initialized to 0.

Upvotes: 4

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

According to the C99 standard, section 6.7.8.17, only the first member (in declaration order, i.e. the a field) is initialized explicitly in your example:

Each brace-enclosed initializer list has an associated current object. When no designations are present, subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order, structure members in declaration order, and the first named member of a union.

By "designation" the standard means

A aa = {.b = 0};

This is a more precise way of letting programmers decide what fields get initialized.

Upvotes: 1

Related Questions