Structures in C++
We often come around situations where we need to store a group of data whether of similar data types
or non-similar data types. We have seen Arrays in C++ which are used to store set of data of similar data
types at contiguous memory locations.
Unlike Arrays, Structures in C++ are user defined data types which are used to store group of items of
non-similar data types.
A structure is a collection of variables of different data types and member functions under a single name.
For e.g. – Suppose you want to store some information about a person: their first_name, last_name, age,
and salary. You can easily create different variables—first_name, last_name, age, salary—to store this
information separately. However, in the future, you might want to store information about multiple
people. Now, you'd need to create different variables for each information per person: first_name1,
last_name1, age1, salary1, first_name2, last_name2, age2, salary2, … You can visualize how big and
messy the code would look. Additionally, as there is no relation between the variables (information), it
would be a daunting task to manage. A better approach is to have a collection of all related information
under a single name, such as Person and use it for every individual. Now, the code looks much cleaner,
more readable, and efficient as well. This collection of all related information under a single name Person
is a structure.
Definition –
A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group
items of possibly different types into a single type. The C++ Course covers how to define and use
structures effectively in C++, helping you manage complex data efficiently.
How to create a structure?
o The ‘struct’ keyword is used to create a structure. The general syntax to create a structure is as
shown below:
struct structureName
member1;
member2;
member3;
……………………
……………………
memberN;
};
How to declare structure variables?
o A structure variable can either be declared with structure declaration or as a separate declaration
like basic types.
// Method – 1 A variable declaration with structure declaration.
struct Point
{
int x, y;
} p1; // The variable p1 is declared with 'Point'
// Method – 2 A variable declaration like basic data types
struct Point
{
int x, y;
};
int main()
{
struct Point p1; // The variable p1 is declared like a normal variable
}
How to initialize structure members?
o Structure members cannot be initialized with declaration. For example, the following C program
fails in compilation.
struct Point
{
int x = 0; // COMPILER ERROR: cannot initialize members here
int y = 0; // COMPILER ERROR: cannot initialize members here
};
o The reason for above error is simple, when a datatype is declared, no memory is allocated for it.
Memory is allocated only when variables are created.
o Structure members can be initialized with declaration in C++. For Example, the following C++
program Executes Successfully without throwing any Error.
// In C++ We can Initialize the Variables with Declaration in Structure.
#include <iostream>
using namespace std;
struct Point
{
int x;
int y;
};
int main()
{
struct Point p1;
// Accessing members of point p1
// Initializing the value of x = 10 & y = 20;
p1.x = 10;
p1.y = 20;
cout << "x = " << p1.x << ", y = " << p1.y<<endl;
return 0;
}
o Structure members can be initialized using curly braces ‘{}’. For example, following is a valid
initialization.
struct Point
{
int x, y;
};
int main()
{
// A valid initialization. member x gets value 0 and y
// gets value 1. The order of declaration is followed.
struct Point p1 = { 0, 1 };
cout << "x = " << p1.x << ", y = " << p1.y<<endl;
return 0;
}
How to access structure elements?
o Structure members are accessed using dot (.) operator.
#include <iostream>
using namespace std;
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = { 0, 1 };
// Accessing members of point p1
p1.x = 20;
cout << "x = " << p1.x << ", y = " << p1.y;
return 0;
}
What is an array of structures?
o Like other primitive data types, we can create an array of structures.
#include <iostream>
using namespace std;
struct Point
{
int x, y;
};
int main()
{
// Create an array of structures
struct Point arr[10];
// Access array members
arr[0].x = 10;
arr[0].y = 20;
cout << arr[0].x << " " << arr[0].y;
return 0;
}