tags:

views:

235

answers:

6

Title pretty much sums it up. When I declare a struct in C, am I guaranteed that the members will be initialized to some specific value, such as 0 for integer members?

EDIT:

So, let's say I have a struct that looks like:

typedef struct
{
     int a;
} my_str;

and I declare:

my_str thing1;

globally. As per some of the answers, thing1.a will be initialized to 0 - am I understanding that correctly?

+7  A: 

You are only guaranteed that variables with static storage duration (global variables, static variables in functions) are initialized to 0. No other guarantees.

hrnt
A: 

No.

For example, the MS VC++ compiler will initialize struct members to zero for debug builds, and will not initialize members to zero for release builds.

Anon E. Mous
Probably should do a search for... "my project works in debug mode but not in release mode with MS VC++". I can't believe their compiler works like that.
blak3r
A: 

No. There is no such guarantee. If you want them to be initialized to zero, it pretty much needs to be done manually, unless the structure variable is declared as global.

EDIT: Yes, I forgot to mention static variables within a function.

Ashwin
A local static variable is also zero-initialized, even though it's not global.
Pavel Minaev
+1  A: 

From the ANSI C standard: If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant. If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate

PiedPiper
+7  A: 

Relevant parts of the C99 standard:

section 6.2.4, §3:

An object whose identifier is declared with external or internal linkage, or with the storage-class specifier static has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.

section 6.2.4, §4:

An object whose identifier is declared with no linkage and without the storage-class specifier static has automatic storage duration.

section 6.2.4, §5 (regarding objects with automatic storage duration):

The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached.

section 6.7.8, §10:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static 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;

— if it is a union, the first named member is initialized (recursively) according to these rules.

Christoph
A: 

In C++ a struct can have a constructor with an initialiser list to perform automatic initialisation.

A struct is a data type, and type does not determine initialisation. Storage class determines initialisation. So a statically allocated object of the struct type will. like any other static object, be initialised to zero. Any object declared with the static keyword or declared outside of a function scope is statically allocated.

Clifford