C++ is the best cross-platform language that is ideal for creating high-performance languages. It is one of the world’s most popular programming languages.

C++ was developed as an extension to the C language by Bjarne Stroustrup. It was updated three times in 2011, 2014, and 2017 with C++11, C++14, and C++17. 

C++ is a part of modern embedded systems, graphical user interfaces, and operating systems. It is an object-oriented language with a clean program structure that lowers development costs and allows the codes to be reused. It is easy to switch between C++ and Java, making it a developer’s favorite.

To start with C++, all you need are a text editor and a compiler. You can use Notepad to write C++ code, and GCC compilers can be employed to translate the C++ code into understandable computer language. An Integrated Development Environment (IDE) can be used to edit and compile C++ codes. 

In this article, we will discuss C++ Struct and learn how to create the instance and access the variable of a structure. We will also dive into the points that distinguish a structure from a class.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

What Is C++ Struct?

Classes and structures hold the collection of data of multiple data types. The structure is the collection of variables of different data types under a single name only. For example, if you require some details about a customer to be stored in the data, you can create the different variables like name, contact number, email id, etc. These variables can be used to store all information separately.

The future scope of data extends to storing information for multiple customers. To fulfill the requirements, you need to create different variables for each customer’s details. For example, name1, contact number1, email id1, name2, contact number2, and email id2. This will create a long code string with no interrelation between the variables like name, contact number, email id, etc.

The best solution to this lengthy coding is collecting all related information under a single customer name like customer1. You can then use it for different customers. This collection of all related information under a single name customer1 yields a clean, efficient, and readable cost and is called structure. The “struct” keyword defines a structure type followed by an identifier.

C++ Struct Syntax

struct structure_name


  // member declarations


The member variables of different types are declared inside the curly brackets. Thus, C++ Struct is declared by preceding the struct keyword and is followed by the structure name.


Struct Student


  char name[20];

  int id;

  int age;


Here, students are the structure containing three variables: name, id, and age. It allocated no memory at the time of declaration of structure. The memory is allocated when the variable of a structure is created.

How to Create the Instance of Structure?

The structure variable can be defined as-

student s;

It is the structure variable of student type. When the structure variable is created, the memory will be allotted according to its size. The student structure here has one char variable and two integer variables. Thus, the total memory occupied will be:

One char variable: 1 byte

Two integer variables: 2*4bytes = 8 bytes

Thus, the total memory occupied by the variable in the above case is 9 bytes.

How to Access the Variable of Structure?

It is easy to access the variable of C++ struct by simply using the instance of the structure followed by the dot (.) operator and the field of the structure.

For example:

s.id= 4;

Here, you’re accessing the id field of the C++ Struct Student by using the dot (.) operator. It assigns the 4 values to the id field.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

Examples of C++ Struct

1. Simple Structure Named Rectangle With Two Data Members: Width and Height

#include <iostream>    

using namespace std;    

 struct rectdemo      


   int w, h;       


int main(void) {    

    struct rectdemo rec;    



   cout<<"Area of Rectangle is: "<<(rec.w * rec.h)<<endl;    

 return 0;    




2. Simple Structure Using Constructor to Initialize Data and Find Area of Rectangle

#include <iostream>    

using namespace std;    

 struct Rectangle    {      

   int width, height;      

  Rectangle(int w, int h)      


        width = w;      

        height = h;      


  void areaOfRectangle() {       

    cout<<"Area of Rectangle is: "<<(width*height); }      


int main(void) {    

    struct Rectangle rec=Rectangle(4,6);    


   return 0;    




Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Differences Between Structure and Class

The differences between the structure and class in C++ are:

Structure in C++

Class in C++

No C++ Struct member can have a null value.

Members can have null values.

The instance of the structure is known as the “Structure variable.”

The instance of the class is known as “Object of the class.”

Memory is allocated on a stack.

Memory is allocated on the heap.

It is considered a value type.

It is considered as a reference type.

The default access specifier will be public in C++ Struct in case it is not declared explicitly.

The default access specifier will be private in the C++ class in case it is not declared explicitly.

Its Syntax is:

struct structure_name


  // member declarations.



Its Syntax is:

class class_name


// body of the class.


Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Wrapping Up

Struct is the lightweight version of data types’ collection and is similar to a C++ class holding different data types. It is easy to create an instance and access the variables in C++ structures. This article covers all about the C++ struct, how to define it, and use it in any program. 

If you are you planning to learn about the practical usability of the C++ struct, you can enroll in  Simplilearn’s Full Stack Web Development course or you can explore Simplilearn’s SkillUp Program that offers free courses on the basics of computer programming. Simplilearn is one of the leading online education providers. Visit Simplilearn now to know about the trending programming skills from the leading mentors of the industry and gain skills and professional expertise with multiple software development professional courses. 

In case you have any questions for us, leave them in the comments section of this article and our experts will get back to you at the earliest.

About the Author


Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors