The structure is a collection of variables of different data types under a single name in c++. It is similar to a class in that, both hold a collection of data of different data types.
Each variable in the structure is known as a member of the structure.
a structure can contain many different data types (int, string, bool, etc.).
To create a structure, use the struct keyword and declare each of its members inside curly braces.
Declaration of Structure :
Two types of Declaration of Structure in c++ :
1) Declaration with variable
struct student // Structure declaration { int roll_no; // Member (int variable) string address; // Member (string variable) } s1; // Structure variable
2) Declaration without variable
struct student // Structure declaration { int roll_no; // Member (int variable) string address; // Member (string variable) }; void main() { student s1; // Structure variable by using student structure name }
Also Read This :- 2d array in c++
Example of Structure :
#include<iostream> #include<conio.h> using namespace std; struct student {   int roll_no;   string name;   string address; }; int main() {   // Create a student structure and store it in s1;   student s1;   s1.roll_no = 101;   s1.name = "ABC";   s1.address = "Palanpur";   // Create another student structure and store it in s2;   student s2;   s2.roll_no = 102;   s2.name = "XYZ";   s2.address = "Sidhpur";   // Print the structure members   cout << s1.roll_no << " " << s1.name << " " << s1.address << "n";   cout << s2.roll_no << " " << s2.name << " " << s2.address << "n";   return 0; }
output
101 ABC Palanpur 102 XYZ Sidhpur