Structure in C++

Structures In C++

You will learn about structures in C++ programming in this article, including what they are, how to define them, and how to utilize them in programs.

We frequently encounter circumstances where we must store a collection of data, whether they are related or unrelated data kinds.

We have seen arrays that are used to store collections of data of related data types in adjacent memory addresses.
In contrast to Arrays, user-defined data types called Structures in C++ are used to store collections of objects with dissimilar data types.

The use of structures, also known as structs, allows you to collect numerous related variables in a single location. A member of the structure is referred to as each variable in it.

A structure, as opposed to an array, can hold a wide variety of data types (int, string, bool, etc.).

Structures are a grouping of variables of various data kinds that go by the same name. In that both hold a collection of data of several data types, it is comparable to a class.

For instance, let’s say you wish to save a person’s name, citizenship number, and salary. To store these pieces of information individually, you can easily construct different variables for name, cityNO, and salary.

You might want to keep data about numerous people in the future, though. For each piece of information per person, you would now need to establish separate variables: Name1, CitNo1, Salary1, CitNo2, Salary2, Name2

It’s simple to picture how voluminous and disorganized the code would be. It will be a difficult task because there would be no relationship between the variables (information).

A preferable strategy would be to group all relevant data under the label Person and apply it to every individual. The code now appears a lot clearer, more understandable, and more effective.

The user-defined data type C++ struct, also known as C++ Structure, is a feature of the C++ language. It enables the user to group data elements of (potentially) several data types under one name.

Due to their ability to store data of various data types, C++ structs differ from arrays in this regard. Arrays can only hold data of similar data types.

Every component of the structure is referred to as a member.

This compilation of all relevant data under one name A person is a building.

  • In C++, a user-defined data type is a structure.
  • In order to gather things of potentially different types into a single line, structures produce a data type.
    In order to gather things of potentially different types into a single line, a structure produces a data type.
  • Because structures can store data of many data types, they vary from arrays, which can only retain data of similar data types.
  • In that both hold a collection of data of several data types, it is comparable to a class.
  • Every component of the structure is referred to as a member.

When programming in C++, how do you declare a structure?

The identifier is followed by a structure type defined by the struct keyword (name of the structure).

You can then define one or more members of that structure inside curly braces (declare variables inside curly braces). For instance:

struct Person
{
char name[50];
int age;
float salary;
};

Here, a person is described as a structure made up of three elements: name, age, and pay.

No memory is allotted when a structure is constructed.

The definition of the structure merely serves as a template for the creation of variables. It is comparable to a datatype. When an integer is defined as follows:

int foo;

The int indicates that only integers can be stored in the variable foo. Similar to this, a structure variable’s specification merely states what property it will have.

Note: Don’t forget to use a semicolon to complete the declaration (;)

How to define a structure variable?

Once a structure person is declared as above. A structure variable could be described as:

Person bill;

Here, a structure variable bill of type structure Person is defined.

The compiler only allocates the necessary memory when a structural variable is specified.

The memory for float is 4 bytes, the memory for int is 4 bytes, and the memory for char is 1 byte, assuming you have a 32-bit or 64-bit machine.

58 bytes of memory are therefore set aside for the structural variable bill.

How to access members of a structure?

The dot (.) operator is used to access the members of a structural variable.

Let’s say you want to add 50 to the age of the structure variable bill. You can complete this task by utilizing the following code.

bill.age = 50;

Example: C++ Structures

A C++ program that displays data assigned to structure variable members.

#include <iostream>
using namespace std;

struct Person
{
    char name[50];
    int age;
    float salary;
};

int main()
{
    Person p1;
    
    cout << "Enter Full name: ";
    cin.get(p1.name, 50);
    cout << "Enter age: ";
    cin >> p1.age;
    cout << "Enter salary: ";
    cin >> p1.salary;

    cout << "\nDisplaying Information." << endl;
    cout << "Name: " << p1.name << endl;
    cout <<"Age: " << p1.age << endl;
    cout << "Salary: " << p1.salary;

    return 0;
}

Output

Enter Full name: Magdalena Dankova
Enter age: 27
Enter salary: 1024.4
Displaying Information.
Name: Magdalena Dankova
Age: 27
Salary: 1024.4

A person structure with three members with their names, ages, and salaries is declared here.

P1 is a structural variable that is defined inside the main() function. The user is then prompted to enter information, after which the information is shown.

These tutorials in C++ are also worth a look After structures:

Functions in C++ with Example | C++ Programming

Variables in C++

Leave a Reply