Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struct initialization of the C/C++ programming language?

Tags:

c++

c

struct

I could do struct initialization with code:

struct struct_type_id struct_name_id = { value1, value2, value3 };

but could not with:

struct struct_type_id struct_name_id;
struct_name_id = { value1, value2, value3 };

why I could do it with the former,but could not with the latter with gcc,g++,vc2008,vc6?In other words,why the c/c++ programming language do not support this syntax?

thanks.

like image 670
Jichao Avatar asked Nov 10 '09 01:11

Jichao


People also ask

What is structure initialization in C?

An initializer for a structure is a brace-enclosed comma-separated list of values, and for a union, a brace-enclosed single value. The initializer is preceded by an equal sign ( = ).

What does it mean to initialize a struct?

When initializing a struct, the first initializer in the list initializes the first declared member (unless a designator is specified) (since C99), and all subsequent initializers without designators (since C99)initialize the struct members declared after the one initialized by the previous expression.

What struct means in C?

In C programming, a struct (or structure) is a collection of variables (can be of different types) under a single name.

Does C initialize structs to 0?

You don't have to initialise every element of a structure, but can initialise only the first one; you don't need nested {} even to initialise aggregate members of a structure. Anything in C can be initialised with = 0 ; this initialises numeric elements to zero and pointers null.


2 Answers

The first statement creates a variable initialized to the given values, i.e., these values are built in memory and stored directly in the program executable in that variable address (for globals) or ready for memory copy (for stack variables).

The second statement of the second block is very different. Although it looks similar, it is an assign expression. It means that the RHS of the equals operator is an expression that is evaluated (independently of what is in the LHS of =), and then passed to the = operator. Without proper context, {...} doesn't have any meaning.

In C99, you can do this:

struct_name_id = (struct struct_type_id){ value1, value2, value3 };

Now the RHS of the equals operator is a valid expression, since there is proper context for the compiler to know what is in {...}.

In C++11, the syntax is:

struct_name_id = struct_type_id{ value1, value2, value3 };
like image 177
Juliano Avatar answered Oct 17 '22 09:10

Juliano


I don't know why C didn't originally support some kind of syntax to 'reinitialize' a struct using something like the initializer list - there are definitely times when I would have found it handy. As Juliano mentioned, C99 (and C++0x) has fixed it to a certain degree, but I often have to stick with C90. When I want to do something like that, I'll sometimes do the following:

struct foo const init_foo = { 1, 2, 3};
struct foo myFoo;

// ....

myFoo = init_foo;  // reinitialize myFoo
like image 37
Michael Burr Avatar answered Oct 17 '22 07:10

Michael Burr