C++ Structures

C++ Structures: In this article, you’ll find out about structures in C++ programming; what is it, how to characterize it, and use it in your program.

The structure is an assortment of factors of various data types under a single name. It is like a class in that, the two hold a collection of information of various data types.

For instance: You need to store some data about an individual: his/her name, citizenship number and pay. You can undoubtedly make various variables name, citNo, salary to store these data separately.

However, later on, you would need to store data about different people. Presently, you’d have to make various factors for every data per individual: name1, citNo1, salary1, name2, citNo2, salary2

You can undoubtedly picture how enormous and chaotic the code would look. Additionally, since no connection between the variables (data) would exist, it will be an overwhelming errand.

A superior methodology will be to have an assortment of all related data under a solitary name Person, and use it for each individual. Presently, the code looks a lot of cleaner, lucid and effective too.

This assortment of all related data under a solitary name Person is a structure.


How to declare a structure in C++ programming?

The struct keyword defines a structure type followed by an identifier (name of the structure).

Then inside the curly braces, you can declare one or more members (declare variables inside curly braces) of that structure. For example:

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

Here a structure person is defined which has three members: name, age and salary.

At the point when a structure is made, no memory is assigned.

The structure definition is just the outline for the making of factors. You can envision it as a datatype. At the point when you characterize a whole number as underneath:

int foo;

The int determines that variable foo can hold the whole number component as it were. Correspondingly, structure definition just determines what property a structure variable holds when it is characterized.

Note: Remember to end the assertion with a semicolon (;)


How to define a structure variable?

Once you declare a structure person as above. You can define a structure variable as:

Person bill;

Here, a structure variable bill is characterized which is of type structure Person.

At the point when structure variable is characterized, at exactly that point the necessary memory is dispensed by the compiler.

Considering you have either 32-bit or 64-bit system, the memory of float is 4 bytes, memory of int is 4 bytes and memory of char is 1 byte.

Hence, 58 bytes of memory is allocated for structure variable bill.


How to access members of a structure?

The members of structure variable is accessed to using a dot (.) operator.

Assume, you need to access the age of the structure variable bill and allot it 50 to it. You can play out this errand by using the following code beneath:

bill.age = 50;

Example: C++ Structure

C++ Program to assign data to members of a structure variable and display it.

#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: salman khan
Enter age: 22
Enter salary: 1050.4

Displaying Information.
Name: salman khan
Age: 22
Salary: 1050.4

Here a structure Person is declared which has three members name, age and salary.

Inside main() function, a structure variable p1 is defined. Then, the user is asked to enter information, and data entered by the user is displayed.


Please feel free to give your comment if you face any difficulty here.