Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

gcc

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!

like image 222
Nan Xiao Avatar asked Mar 15 '13 09:03

Nan Xiao


1 Answers

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.

like image 51
µtex Avatar answered Oct 01 '22 07:10

µtex