Latest Code Tutorials

C++ Structure Example | Structure in C++ Tutorial


C++ Structure is a collection of different data types under the same name. Those data elements mean roll no, marks are named as members of the structure. Structures in C++ are user-defined data types that are used to store a group of items of non-similar data types.

Suppose, you are given a task to store the academic information of a student and make a program for that. One thing you can do is, you can take different data types according to the requirements, as an array to store the marks, a string to take a name, int to take roll number, etc. In this case, there can be a problem, like accessing data. But what if you are given a method where you can store them all together? 

C++ Structure Example

C++ allows us to create our own user-defined aggregate data types. An aggregate data type is a data type that groups multiple individual variables together. One of the simplest aggregate data types is the struct. A struct (short for structure) allows us to group variables of mixed data types together into a single unit.

You can think of a box, where inside it all your favorite foods are stored. The structure is also the same, inside it all our requirements are stored.

#Structure declaration

You can declare the structure using the struct keyword.

struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
} object_names;

Here, structure_name is the name of the structure you will provide. The object_name is to identify the structure inside the main(). And the member_types are the data members we will use, like roll number, name, etc.

struct Student
   char name[10];
   int roll_number, total_marks;
   float average;
} stn;

Remember, the structure declaration is only a declaration; no memory is allocated when we are declaring a structure. This is like a blueprint, means how the structure will look like.

Always remember, never forget to give semicolon (;) at the end of structure declaration. 

#Defining a structure variable (object)

Structure variable is a variable that is always written inside the main() to using which we can access members of the structure. Structure variable and Object_name are the same. You can declare it when declaring the structure or you can declare it inside main(). But structure can be declared inside the main() or outside the main() also. 

When a structure variable is defined, then only the memory is allocated. We can define a structure variable of the structure Student inside main like the following.

student stn;

For the above example, if we consider the size of a float is 4 byte, the size of int is 4 byte, and the size of char is 1byte. Then the total size of the structure variable (stn) will be the (1*10)+(4*2)+(4*1)= 22 bytes.

You can declare multiple structure variable to a structure. Like Class and Objects, each variable will also different data for the same structure member.

For example, we can write in main()

student class1, class2;

#Access members of a Structure

Suppose you want to access the data of roll_no of structure Student ( defined above). To access any member of a structure, you have to use the dot( . ) operator. See the following syntax.


For example, to access the roll number of above defined Student Structure, you have to write the following.


For example, to access the roll number of above defined Student Structure, you have to write the following code.


Also, for multiple structure variables, we can write like the following.


Refer to the below code for better understanding.

See the following program.

#include <iostream>
using namespace std;
struct student
    string name;
    int roll, marks;
    float avg;
int main()
    student cl1, cl2; //declaring structure object

    //Assigning values to structure members using object = "Debasis";
    cl1.roll = 15;
    cl1.marks = 541;
    cl1.avg = 83; = "Ayush";
    cl2.roll = 12;
    cl2.marks = 654;
    cl2.avg = 92;
    //Printing the values of structure members
    cout << "\nName of the student of Class1 is: " <<;
    cout << "\nRoll: " << cl1.roll;
    cout << "\nMarks: " << cl1.marks << " Average: " << cl1.avg;

    cout << "\n\nDetails of the student of class2:\n";
    cout << "Name: " << << "\nRoll: " << cl2.roll << "\nMarks: " << cl2.marks;
    cout << "\nAverage: " << cl2.avg << "\n";
    return 0;

See the following output.

C++ Structure Example

#Structure and Pointer

Like other types, the structure also can be pointed to by its type of pointer.

See the following example.

struct Fruit
	string name;
	string colour;

And in main() if we write the following.

Fruit fr;
Fruit* pfr;

Here, fr is an object of structure Fruit but pfr is a pointer pointing to the object of structure Fruit. So, after that, the following code should be written.


By this, the address of fr will be assigned to pfr. Refer to the below coding example for better understanding.

using namespace std;
struct fruit
    string name,colour;
    int weight;
int main()
    //Using multiple
    fruit f1,f2;
    fruit* pft;
    fruit* pf2;
    string s;
    //Member can be accessed by -> also
    cout<<"\nName of first fruit is: "<<pft->name;
    cout<<"\nColour of banana is: "<<pft->colour;
    cout<<"\nColour of Apple is: "<<pf2->colour;
    return 0;

See the following output.

Structure and Pointer

#Structure and Function

We can assign functions to the members of a structure and then can use them in Function. See the following example.

using namespace std;
struct voter
    string name,add;
    int id;
//declaring function
//structure object is the parameter
void display(struct voter v)
    cout<<"\nVoter ID: "<<;
    cout<<"\nName: "<<;
    cout<<"\nAddress: "<<v.add<<"\n";
int main()
    voter v1;;"Debasis Jana";
    v1.add="Kolkata, West Bengal";
    //calling the function using structure object
    return 0;

See the following output.


#Nested Structure

A nested structure means, structure inside a structure, same as nested if-else statements. So, here also we can declare a structure inside a structure and then can use them.

struct structure1

struct structure2
    struct structure1 obj;	

The above syntax tells that, structure1 is nested to structure2.

#Accessing members of nested structure

In main() we have first to declare the object of structure 2, and the object of structure1 is already proclaimed in structure2.

	structure2 st2;
	//To access member of structure2, just simply write
	//To access member of structure1, we have to write

You will understand the concept properly by the following example below.

using namespace std;
struct salary
    int DA,TA,Total;
struct Employee
    string name,add;
    //declaring structure salary as a nested structure
    struct salary s;
void display(struct Employee E1)
    cout<<"\nName: "<<;
    cout<<"\nAddress: "<<E1.add;
    cout<<"\nDA+TA: "<<((E1.s.DA)+(E1.s.TA));
    cout<<"\nTotal Salary: "<<((E1.s.DA)+(E1.s.TA)+(E1.s.Total))<<"\n";

int main()
    Employee E;
    //adding values to the member of Employee structure"Ankit";
    //Adding values to the member of salary structure
    //calling display function


See the following output.

Accessing members of nested structure

So, here we end the concept of C++ Structure Example | Structure in C++ Tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.