How To Define A Struct in C++

Today We will be understanding how to define a struct in C++.
So Read On…

How To Define A Struct in C++

Table of contents:

  • What is struct
  • Syntax and example of a struct
  • Defining and accessing members
  • Sample program on how to define a struct in C++

What Is A Struct?

A Struct is a shorthand for structure.
It is a user-defined data structure that handles a collection of data with different data types.
We can create multiple instances of a Structure.
When an instance is created, space is allocated for the variables inside the structure.
The amount of space allocated is equal to the sum of all the sizes of variables in the structure.
Since each variable has a separate space, we can access them simultaneously.
Drawback is that we need a memory that can allocate that much space.
We use the keyword “struct” while defining a structure.

Syntax And Example:

Syntax:
struct <structure_name>
{
<data_type> <variable>;
<data_type> <variable>;
<data_type> <variable>;
};

NOTE: Do not forget the semicolon at the end.

Example:
struct abcd
{
int a;
float b;
char c[50];
};

But an important parameter while coding is the amount of space the program takes.
Struct gives space to every single variable.

Consider the above example on a 64bit system.

Variable Space = sizeOf(int a) + sizeOf(float b) + sizeOf(char c[50])
Therefore          =>4 bytes + 4 bytes + 1 byte*50
Therefore          =>58 bytes.

Hence 58 bytes are allotted just for the above example.

Defining and Accessing members:

Once a struct is created, we need to use those variables too.

An instance of a struct is defined using the following syntax:
<struct_name> <struct_variable>;

The struct_variable is is an instance of struct and the moment the compiler reads this line, it allocates the required space.
We can create multiple instances of the same struct but we also need that much free memory.

To access a variable in the struct, we use the following synntax:
<struct_variable>.<variable>;
Here the variable is defined in the struct.

Consider the above example of struct abcd to understand defining and accessing members.

To define:
abcd inst;
To access:
inst.a=5;

Code Sample: Struct in C++

Take a look at this code:

#include <iostream>
using namespace std;

struct abcd
{
    long int a;
    float b;
    char c[50];
};

int main()
{
    abcd inst1,inst2;
    
    cout<<"Name: ";
    cin>>inst1.c;
    cout<<"Marks: ";
    cin>>inst1.b;
    cout<<"Phone no: ";
    cin>>inst1.a;

    cout<<"\n";
    
    cout<<"Name:";
    cin>>inst2.c;
    cout<<"Marks:";
    cin>>inst2.b;
    cout<<"Phone no.";
    cin>>inst2.a;
    
    cout<<"\n\nDatabase is:\n\nName\t|\tPh. No.\t|\tMarks\t|\n";
    cout<<inst1.c<<"\t|\t"<<inst1.a<<"\t|\t"<<inst1.b<<"\t|\t\n";
    cout<<inst2.c<<"\t|\t"<<inst2.a<<"\t|\t"<<inst2.b<<"\t|\t";    
}

Let us have a look at it line by line.

  1. struct abcd
    {
        long int a;
        float b;
        char c[50];
    };

    Here I am defining a struct called abcd and it has 3 variables in it.
    long int a  ->To store a phone number.
    float b        ->To store marks.
    char c[50] ->To store a name.

  2. abcd inst1,inst2;

    Now I am creating 2 instances of the same structure called inst1 and inst2.
    Each instance will store a different person’s data.

  3. cout<<"Name: ";
    cin>>inst1.c;
    cout<<"Marks: ";
    cin>>inst1.b;
    cout<<"Phone no: ";
    cin>>inst1.a;
    

    Here I am accepting the data from the user and storing it in a particular instance called inst1.
    I have successfully got the data of a particular person.

  4. cout<<"Name:";
    cin>>inst2.c;
    cout<<"Marks:";
    cin>>inst2.b;
    cout<<"Phone no.";
    cin>>inst2.a

    Now using the second instance, inst2, we are getting the second user’s data.

  5. cout<<"\n\nDatabase is:\n\nName\t|\tPh. No.\t|\tMarks\t|\n";
    cout<<inst1.c<<"\t|\t"<<inst1.a<<"\t|\t"<<inst1.b<<"\t|\t\n";
    cout<<inst2.c<<"\t|\t"<<inst2.a<<"\t|\t"<<inst2.b<<"\t|\t";

    With the help of tabs and the next line, we are creating a table like a view to show this data.
    It may look complex but just cut out the tabs and next lines and you’ll see that we are doing nothing but printing values and a “|” between them

Here is the output of my code:

Name: Tony
Marks: 99
Phone no: 1234                                                                                                                                    

Name:Drax
Marks:10
Phone no.9876
                                                                                                                                             

Database is:

Name    |       Ph. No. |       Marks   |
Tony    |       1234    |       99      |
Drax    |       9876    |       10      |

Hence we have understood how to define a struct in C++.

Leave a Reply

Your email address will not be published.