Skip to content

Structures

A structure is a user-defined data type in C++ that allows you to group different types of data under a single name. Structures are used to represent a record, which is a collection of related data fields. Each data field in a structure is called a member.

In layman’s terms, you can think of a person as a structure. A person has attributes such as name, age, and balance. Similarly, a structure in C++ can have members like name, age, and balance. These members can be of different data types, such as int, float, char, and more.

Declaring Structures

To declare a structure in C++, you need to use the struct keyword followed by the structure name. Here is the syntax of declaring a structure:

struct structure_name {
data_type member1;
data_type member2;
data_type member3;
// Add more members here
};

Here is an example of declaring a structure called Person with members name, age, and balance:

struct Person {
string name;
int age;
float balance;
};

In the example above:

  • Person is the name of the structure.
  • string, int, and float are the data types of the members.
  • name, age, and balance are the members of the structure.

Accessing Structure Members

You can access the members of a structure using the dot . operator. Here is an example of accessing the members of the Person structure:

Person p;
p.name = "Abby";
p.age = 30;
p.balance = 1000.50;
cout << "Name: " << p.name << endl;
cout << "Age: " << p.age << endl;
cout << "Balance: " << p.balance << endl;

In the example above, we create a variable p of type Person and assign values to its members name, age, and balance. We then print the values of these members using the cout object.

Nested Structures

You can also have structures within structures, known as nested structures. Nested structures are useful when you want to represent complex data structures. Here is an example of a nested structure:

struct Address {
string street;
string city;
string state;
int zipCode;
};
struct Employee {
string name;
int age;
float salary;
Address address;
};
Employee e;
e.name = "Rob";
e.age = 35;
e.salary = 50000.0;
e.address.street = "123 Main St";
e.address.city = "Anytown";
e.address.state = "NY";
e.address.zipCode = 12345;
cout << "Name: " << e.name << endl;
cout << "Age: " << e.age << endl;
cout << "Salary: " << e.salary << endl;
cout << "Address: " << e.address.street << ", " << e.address.city << ", " << e.address.state << " " << e.address.zipCode << endl;

In the example above, we create a structure Address with members street, city, state, and zipCode. We then create a structure Employee with members name, age, salary, and address, where address is of type Address. We assign values to the members of the Employee structure and print them using the cout object.

Arrays of Structures

You can create arrays of structures in C++. This allows you to store multiple records of the same type in a single data structure. Here is an example of an array of structures:

struct Student {
string name;
int rollNumber;
float marks;
};
Student students[3];
students[0].name = "Abigail";
students[0].rollNumber = 101;
students[0].marks = 85.5;
students[1].name = "Bob";
students[1].rollNumber = 102;
students[1].marks = 90.0;
students[2].name = "Charlie";
students[2].rollNumber = 103;
students[2].marks = 75.0;
for (int i = 0; i < 3; i++) {
cout << "Name: " << students[i].name << endl;
cout << "Roll Number: " << students[i].rollNumber << endl;
cout << "Marks: " << students[i].marks << endl;
}

In the example above, we create an array of structures Student with a size of 3. We assign values to the members of each structure in the array and print them using a for loop.

Structures are a powerful feature of C++ that allow you to represent complex data structures in a simple and organized way. I would recommend using structures whenever you need to group related data fields together, instead of creating multidimensional arrays or using separate variables for each field. This will make your code more readable and maintainable.