C++ Struct | Syntax | How does C++ struct function with Examples

C-plus-plus-StructC-plus-plus-Struct

Introduction to C++ Struct

A struct in C++ is a data structure that allows forming a new type of datatype by combining multiple different types of primitive data types available in C++. We can form a new kind of datatype in addition to existing primitives by combining them as per the application requirement. This is known as a structure in C++.  As arrays allow us to store the same type of data types, the structure allows us to a group and combine the different types of data types. The structure makes it easy to map real-life entities or data models with storage and handling in C++.

Syntax

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

struct name_of_structure {
// Multiple variables of different data types
}

The syntax of structure in C++ is very easy to understand and use. It starts with the keyword “struct” followed by the name of a structure. In the curly braces, we can add multiple variables with different data types. The name of the structure now can be considered as a new type of data type which is available for use. How to use structure including accessing of data or each member variable in structure, etc. we will see in the examples.

How does C++ struct function?

Let’s understand this by looking at how a structure is stored in memory internally. Just like memory is allocated for primitives such as char, int in C++ with their default size, in the same way, it is allocated for the structure as well. As the structure is a combination of different member variables, the memory allocated will be the sum of memory required for each variable. Now we have memory allocated in this way and a reference to this memory will be returned when we create a new member of a structure type. Then the individual member can be accessed by using the (.) dot operator between structure name and a member name, which will eventually point to a member variable.

Examples of  C++ Struct

Below are the examples:

Example #1

Defining structure and accessing member variables

Code:

#include <iostream>
using namespace std;
// Declaring struct of type Car
struct Car {
string brand ; // Member variables. . .
string model ;
string color ;
int seats;
};
int main()
{
Car c1; // Line 1 Creating variable of type Car
c1.brand = "Honda"; // Line 2 Setting up the Member variable values. . .
c1.model = "City";
c1.color = "White";
c1.seats = 5;
cout << " Car details are as below: \n " << endl;
cout << " brand: " << c1.brand << endl; // Line 3 Accessing the memeber variables
cout << " model: " << c1.model << endl; // using . (Dot) operator
cout << " color: " << c1.color << endl;
cout << " Seat Capacity: " << c1.seats << endl;
return 0;
}

Output:

C-plus-plus-Struct1C-plus-plus-Struct1

Here, we have defined a struct named Car with member variables like brand, model, etc. In this way,  struct makes it easy to define an entity with multiple parameters into a single datatype. The new variable of type Car is defined inline 1, then the field members are set at line 2 and finally, they are accessed at line 3. Note the use of dot operator over here.

Example #2

Defining the struct variable at the time of definition

Code:

#include <iostream>
using namespace std;
// Declaring struct of type Car
struct Car {
string brand ; // Member variables. . .
string model ;
string color ;
int seats;
} c1; // Declare variable at the time of definition
int main()
{
// Car c1; // Creating variable of type Car
c1.brand = "Honda"; // Setting up the Member variable values. . .
c1.model = "City";
c1.color = "White";
c1.seats = 5;
cout << " Car details are as below: \n " << endl;
cout << " brand: " << c1.brand << endl; // Accessing the memeber variables
cout << " model: " << c1.model << endl; // using . (Dot) operator
cout << " color: " << c1.color << endl;
cout << " Seat Capacity: " << c1.seats << endl;
return 0;
}

Output:

example2example2

Here, the variable of type Car is declared at the time of definition of structure. This is another way of declaring a struct variable. This type of declaration can be used if we already know the struct member or we want to use it globally.

Example #3

Initialization using curly braces

Code:

#include <iostream>
using namespace std;
// Declaring struct of type Car
struct Car {
string brand ; // Member variables. . .
string model ;
string color ;
int seats;
};
int main()
{
Car c1 = { "Honda", "Amaze", "Red", 5}; // Line 1
cout << " Car details are as below: \n " << endl;
cout << " brand: " << c1.brand << endl; // Accessing the memeber variables
cout << " model: " << c1.model << endl; // using . (Dot) operator
cout << " color: " << c1.color << endl;
cout << " Seat Capacity: " << c1.seats << endl;
return 0;
}

Output:

C-plus-plus-Struct3 C-plus-plus-Struct3

In the previous example, we had set the member variables by accessing them individually. Here at line 1, we have initialized them all at once at the time of declaration using curly braces.

Example #4

Using constructor and method with struct. We can also define constructor and methods instruct, below is the demonstration of it.

Code:

#include <iostream>
using namespace std;
// Declaring struct of type Car
struct Car {
string brand ; // Member variables. . .
string model ;
Car( string b, string m ) {
brand = b;
model = m;
}
void displayDetails(){
cout << " brand: " << brand << endl;
cout << " model: " << model << endl;
}
};
int main()
{
Car c1 = Car("Honda", "Amaze");
cout << " Car details are as below: \n " << endl;
c1.displayDetails();
return 0;
}

Output:

Example 4Example 4

Conclusion

A struct in C++ is a structure that allows defining user-defined data types using multiple primitive data types. There are multiple ways in which struct can be declared, initialized and used. In this article, we have seen most of them.

Recommended Articles

This is a guide to C++ Struct. Here we discuss the introduction, how do C++ struct function, syntax and the examples with outputs. You can also go through our other suggested articles to learn more –

0

Shares

Share