Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Structure Initialization

Tags:

c++

Is it possible to initialize structs in C++ as indicated below

struct address {     int street_no;     char *street_name;     char *city;     char *prov;     char *postal_code; }; address temp_address =     { .city = "Hamilton", .prov = "Ontario" }; 

The links here and here mention that it is possible to use this style only in C. If so why is this not possible in C++? Is there any underlying technical reason why it is not implemented in C++, or is it bad practice to use this style. I like using this way of initializing because my struct is big and this style gives me clear readability of what value is assigned to which member.

Please share with me if there are other ways through which we can achieve the same readability.

I have referred the following links before posting this question

  1. C/C++ for AIX
  2. C Structure Initialization with Variable
  3. Static structure initialization with tags in C++
  4. C++11 Proper Structure Initialization
like image 890
Dinesh P.R. Avatar asked Jul 17 '12 05:07

Dinesh P.R.


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 ( = ).

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.

Can we initialize variable in structure in C?

The direct answer is because the structure definition declares a type and not a variable that can be initialized. Your example is: struct s { int i=10; }; This does not declare any variable - it defines a type.

Can you initialize values in a struct?

No! We cannot initialize a structure members with its declaration, consider the given code (that is incorrect and compiler generates error).


2 Answers

If you want to make it clear what each initializer value is, just split it up on multiple lines, with a comment on each:

address temp_addres = {   0,  // street_no   nullptr,  // street_name   "Hamilton",  // city   "Ontario",  // prov   nullptr,  // postal_code }; 
like image 85
Wyzard Avatar answered Sep 28 '22 06:09

Wyzard


After my question resulted in no satisfying result (because C++ doesn't implement tag-based init for structures), I took the trick I found here: Are members of a C++ struct initialized to 0 by default?

For you it would amount to do that:

address temp_address = {}; // will zero all fields in C++ temp_address.city = "Hamilton"; temp_address.prov = "Ontario"; 

This is certainly the closest to what you wanted originally (zero all the fields except those you want to initialize).

like image 40
Gui13 Avatar answered Sep 28 '22 06:09

Gui13