एक array of structure दो अलग-अलग concept का combination है, जो निम्नलिखित हैं:
C ++ में array of structure का प्रयोग करके, हम किसी array के सभी elements को एक structure type के element बना सकते हैं, जिसका अर्थ है कि array का प्रत्येक elements एक structure type member होंगे।
जैसा कि हम जानते हैं कि structure अलग-अलग data -types को store कर सकता है परन्तु array of structure से array में हम अप्रयक्ष रूप से different data types को स्टोर कर सकते हैं।
एक तरह से कह सकते है कि हम array of structure में एक structure – variable को array -variable बनाते हैं।
इसे आप नीचे दिए गए diagram की सहायता से समझ सकते हैं-
यहाँ arr[4] एक structure – variable है जो एक array type का है।
जैसा कि आप ऊपर देख सकते हैं, structure -variable के नाम एक जैसे हैं लेकिन अलग-अलग index value के कारण, structure -variable एक दूसरे से अलग हैं। ध्यान रहे array ने same data type (केवल structure types data ) को ही store किया है और जैसे हम जानते है कि structure different data type store कर सकता है इसलिए यह अप्रत्यक्ष रूप है।
declaration of a structure-variable
इसका syntax नीचे दिया गया है –
structure_name structure_variable[size];
इसमें हम सबसे पहले structure -members declare करते हैं फिर structure -variable को एक array प्रकार का variable बनाते हैं-
struct student{
int roll,age
char name[10];
};
book vr[2];
उपरोक्त structure declaration में, हमने array of structure का उपयोग करके दो students के record (roll , age , name) store किए हैं।
For 1st student
vr[0].roll;
vr[0].age;
vr[0].name;
For 2nd student,
vr[1].roll;
vr[1].age;
vr[1].name;
different index value structure के different variable को दर्शाता है क्योंकि यहाँ पर हमने array size 2 declare किया है इसलिए यह दो students के record को store करेगा।
इसका program नीचे दिया गया है –
accessing member in an array of structure
क्योंकि इसमें सभी array elements, structure प्रकार के होंगे इसलिए –
structure-variable [value].structure-member-name
यहाँ पर value index value को दर्शाता है। चलिए इसे नीचे program से समझते हैं-
Program of array of structure in c++
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
// struct declaration outside main()
struct
{
int roll,age;
char name[20];
}vr[2];
void main()
{
clrscr();
for(int i=0; i<2; i++)
{
cout<"Enter "<<i+1<<" Student Record\n";
cout<<"Enter Roll no: ";
cin>>vr[i].roll;
cout<<"Enter Name : ";
gets(vr[i].name);
cout<<"Enter age : ";
cin>>vr[i].age;
}
cout<<endl;
for(int j=0; j<2; j++)
{
cout<<"\nDisplay "<<j+1<<" Student Record";
cout<<"\nRoll no.: "<<vr[j].roll;
cout<<"\nName : "<<vr[j].name;
cout<<"\nAge : "<<vr[j].age;
}
getch();
}
OUTPUT
Enter 1 Student Record
Enter Roll no: 10
Enter Name : Rahul sherma
Enter age : 25
Enter 2 Student Record
Enter Roll no: 11
Enter Name : Rohit kumar
Enter age : 24
Display 1 Student Record
Roll no.: 10
Name : Rahul sherma
Age : 25
Display 2 Student Record
Roll no.: 11
Name : Rohit kumar
Age : 24
क्योंकि C ++ में class और structure कई मामलों में एक-दूसरे से मेल खाते हैं। इसलिए, array of structure और array of object लगभग समान हैं।
Related exercise
- array of object in C++
- Array Of Object Examples In C++
- Bank Management System In C++
- To found out structure size in C++
- Storing student personal and subjects details using a structure
- copy structure and nested structure in C++
- array of structure in C++
previous- structure in c++ hindi
next- union in C++ hindi