C++

How to Initialize struct and union in C++?

C++ is a popular object-oriented programming language for creating software. The structs and unions are two important data types in C++ that allow you to group different elements of data into a single unit.

In this guide, we will discuss struct and union initialization, one of the important concepts in C++ programming.

Struct Initialization

Structs are user-defined data types that allow you to group different elements of data into a single unit. A struct can contain different data types, including other structs, arrays, and pointers. Initialization of structs in C++ is similar to the initialization of other data types such as integers, floats, and strings.

Structs can be initialized both with curly braces and the equal sign.

1: Curly Brace Initialization

The curly brace initialization can be used to initialize all members of a struct in a single line of code. For example, suppose we have a struct called person with three members, name, age, and gender. We can initialize this struct with curly braces as shown below:

struct person {
string name;
int age;
char gender;
};
person student = {"John", 20, 'M'};

The above statement initializes a new instance of the person struct and assigns values to its members. The sequence in which the members are given corresponds to how they are specified in the struct.

2: Equal Sign Initialization

You can also use the equal sign initialization to initialize a single member of a struct. For example, if to initialize only the name of the person struct, you can use the following code:

person teacher;
teacher.name = "Mary";

Union Initialization

Another personalized data type that enables you to combine many data types into one entity is a union. However, unlike structs, a union can only have one member instantiated at a time. Like structs, unions can also be initialized using curly braces or the equal sign. However, unlike structs, unions can only store one value at a time. As a result, a union can only initialize its first member.

1: Curly Braces Initialization

For example, suppose we have a union called numbers with two members, x and y. We can initialize this union using curly braces as shown below:

union numbers {
int x;
float y;
};
numbers num1 = {10};

In the above example, only the x member of the numbers’ union is initialized with the value 10.

2: Equal Sign Initialization

Instead of specifying a value for the y member, we can use an equal sign to initialize, as shown below:

numbers num1 = 43;

The value 43 will be allocated to the union’s initial member, which in this case is the num1 member.

It’s worth noting that if you want to initialize a struct or union with all zeroes, you can use the shorthand syntax of assigning it to an empty pair of braces. For example, the following statement initializes a struct called Point with all zeroes:

Point p = {};

There is another way to initialize structs and unions, which is known as Designated Initializers.

Designated Initializers

You can also use designated initializers to assign values to specific members of a struct or union. This is especially useful when you have a struct or union with many members, and you want to initialize only a few of them. For example, suppose you have a struct called Rectangle that contains members for its height and width. You could initialize this struct and assign values only to its width member by using the following syntax:

Struct Rectangle{
int length;
int width;
}
Rectangle r = {.width = 10};

In this statement, only the width member of the Rectangle struct is initialized, and its height member is left at its default value of 0.

Conclusion

Struct and union initialization is an important aspect of C++ programming. It can be done using curly braces or the equal sign. Curly braces initialization can be used to initialize all members of a struct in a single line of code. However, curly brackets can only be used to initialize the first union member. On the other hand, the equal sign initialization can be used to initialize a single member of a struct or union.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.