Just as an array is a collection of similar data types, the structure is a collection of similar and different data types.
Let us explore it
Difference between structure and array in C++
As we know in array, we can store only one type of data -type at a time, whereas in structure we can store different data -type at one time.
As you can see in the picture below,
In an array –
Meaning, only the same types values (int types here) will be stored in the array at the same time while the structure has different data types stores at the same time. Like,
In addition, an array can also be a member of a structure as you see in the above diagram .
structure are user defined types. In which structure name is an identifier i.e. any user defined name (except reserved keyword).
struct student {
.........;
.........;
};
Here student is an identifier and struct is a reserved keyword.
The data types in the structure are declared within the parentheses bracket. Those are called data -member and each member terminates with semicolon.
struct student {
int roll;
char name[20];
};
In C ++, members of a structure can be both data type and function type-
struct student {
int roll;
void putdata();
};
The structure will reserve memory only after the structure -variable is declared. To access members of a structure, a structure-variable with member name will be used,
structure structure-variable;
structure-variable.roll;
structure-variable.putdata()
The structure reserves different space for each member, (the size of the member depends on their data -type), which means that the memory address of all the members will be different. As you can see in the diagram below,
In this way, the stored values do not affect each other due to the different memory address in the structure.
Compare this with the diagram of union.
C++ structure member declaration
To make a variable a structure type, we use the struct keyword which is a reserved keyword.
syntax
storage-class struct structure-name {
data member1;
data member2;
data member n;
};
Where structure-name is an identifier that would be optional and data-member means data-types (int, float, char, array).
struct {
int sr;
char name[10];
float price;
};
declaration of a structure variable in C++
To access members of a structure, we declare structure-variable. Note that there can be more than one structure -variable with different name of one structure.
First case: when the structure is declare inside main () function-
struct {
int sr_no;
char name[10];
float price;
} vr1,vr2,v3;
2nd case: when the structure is declared outside of main() function-
struct structure-name {
int sr_no;
char name[10];
float price;
};
structure-name vr1,vr2,vr3;
Where vr1, vr2, vr3 are three different structure -variable.
accessing the data member of a structure
Each member of the structure is accessed by a structure variable. We can access structure member with two types of operator-
- using dot operator (.)
- using arrow operator (->)
As already stated, more than one structure of a structure can be -variable but with different name as mentioned above.
When we access members of a structure from the arrow operator, we have to make the structure -variable a pointer type variable, that is, you are using pointer to access the members of the structure. Therefore, a structure, we usually use dot operator.
syntax
structure-variable.member-name;
Where “.” is a dot operator.
program is given below –
Example of structure in C++
#include<iostream>
#include<stdio> // for gets()function
using namespace std;
int main()
{
struct
{ // struct declaration as local
int sr_no;
char name[20];
float price;
} vr;
clrscr();
cout<<"Enter Serial No.: ";
cin>>vr.sr_no;
cout<<"Enter Book Name : ";
gets(vr.name);
cout<<"Enter Book Price: ";
cin>>vr.price;
cout<<"\nSerial No.: "<<vr.sr_no;
cout<<"\nBook Name : "<<vr.name;
cout<<"\nBook Price: "<<vr.price;
return 0;
}
OUTPUT
Enter Serial No.: 101
Enter Book Name : C++ Programming
Enter Book Price: 350
Serial No.: 101
Book Name : C++ Programming
Book Price: 350
Initialization of a structure in C++
As we know structure-name is an optional but when we initialize structure, structure-name becomes mandatory.
The initialization of the structure is slightly different from the normal variable. In this case, the structure member is declared first, which is inside the bracket and then the structure variable is declared outside the bracket.
Now from the same structure -variable, structure members initialize in the order in which they were declared.
Its syntax is given below-
structure-name structure_variable = { members-list } vr;
Here vr denotes a structure-variable, a structure can have more than one structure -variable.
Example
Let us understand this with an example,
First, the structure is declare,
struct book = {
int sr,
char name[20],
float price
} vr;
then values are assigned to the member in the same order from structure-name and structure-variable-
book vr = {
11,
"tutorial" ,
230.50
};
And as we know the members of this structure are accessed from structure-variable, so –
vr.sr; // 11
vr.name; //tutorial
vr.price;//230.50
It is given in the program below –
#include<iostream.h>
#include<conio.h>
struct book { // structure declare outside of void main
int sr;
char name[20];
float price;
};
book vr = {
11,
"C++ Tutorial",
230.50
};
void main()
{
clrscr();
cout<<"Serial No.: ";
cout<<vr.sr;
cout<<"\nBook Name : ";
cout<<vr.name;
cout<<"\nBook Price: ";
cout<<vr.price;
getch();
}
OUTPUT
Serial No.: 11
Book Name : C++ Tutorial
Book Price: 230.5
Related Exercise
more about structure in C++
- copy structure in C++
- nested structure in C++
- array of structure in C++
- structure with function in C++
- similarities between structure and union in C++
- Difference between union and structure in C++
- Difference between class and structure in C++
- similarities between class and structure in C++
Previous – array in C++ with examples
Next- union in C++ with examples