जिस प्रकार एक array समान data types का collection होता है, उसी प्रकार structure sameऔर different data types का collection होता है।
आइये इसे विस्तार से समझते हैं –
Difference between structure and array in C++ in hindi
जैसे की हम जानते हैं array में, हम एक समय में केवल एक प्रकार के data -type को ही store कर सकते हैं, जबकि structure में हम एक समय में different data -type को एक साथ store कर सकते हैं।
जैसा कि आप नीचे चित्र में देख सकते हैं,
एक array में –
का अर्थ है, केवल array में एक समय पर same types values(यहाँ पर int
types ) ही store होंगे जबकि structure में एक साथ different data types store होते हैं। जैसे कि,
इसके अलावा, एक array एक structure का member भी हो सकता है जैसा कि ऊपर diagram में बताया गया है।
Rule of declaring a structure in C++
- structure user defined types होते हैं। जिसमे structure name नाम एक identifier है यानी कोई भी user defined name (reserve keyword को छोड़कर)।
struct student {
.........;
.........;
};
यहां student एक identifier है और struct
एक reserve keyword.
- structure में data types parentheses bracket के भीतर declared किये जाते हैं। जिन्हे data -member कहा जाता है और प्रत्येक member semicolon से terminate होता है।
struct student {
int roll;
char name[20];
};
- C ++ में, किसी structure के member data type और function type दोनों हो सकते हैं।
struct student {
int roll;
void putdata();
};
- structure केवल structure -variable declare होने के बाद ही memory reserve करेगा। किसी structure के members को access करने के लिए member name के साथ structure variable का प्रयोग किया जाता है।
structure structure-variable;
structure-variable.roll;
structure-variable.putdata()
- structure प्रत्येक members के लिए memory अलग-अलग space reserve करता है, (member का size उनके data -type पर निर्भर करता है), कहने का मतलब है, सभी members memory address अलग अलग होता है।
जैसा कि आप नीचे दिए गए diagram में देख सकते हैं,
इस तरह, structure के memory के अलग-अलग memory address होने के कारण stored values एक-दूसरे को प्रभावित नहीं करते। इसे आप union के diagram से compare करें- union in C++
C++ structure member declaration
किसी variable को structure type बनाने के लिए हम struct
keyword का प्रयोग करते हैं जो कि एक reserve keyword है।
SYNTEX
storage-class struct structure-name {
data member1;
data member2;
data member n;
};
जहाँ structure-name एक identifier है जो optional होता और data-member से मतलब data-types (int, float, char, array)
से है।
struct {
int sr;
char name[10];
float price;
};
declaration of a structure variable in C++
किसी structure के members को access करने के लिए हम structure-variable declare करते हैं। एक structure के different नाम से एक से अधिक structure -variable हो सकते हैं।
first case : जब structure void main()
के अंदर declare किया जाता है।
struct {
int sr_no;
char name[10];
float price;
} vr1,vr2,v3;
2nd case : जब structure void main()
के बाहर declare किया जाता है।
struct structure-name {
int sr_no;
char name[10];
float price;
};
structure-name vr1,vr2,vr3;
जहां vr1, vr2, vr3 तीन different structure -variable हैं।
accessing the data member of a structure
structure के प्रत्येक member को structure variable से access किया जाता जाता है। हम दो प्रकार के operator के साथ structure member को access कर सकते हैं-
- dot operator (.) का उपयोग करना
- arrow operator (->) का उपयोग करना
याद रखें कि एक structure के एक से अधिक structure -variable हो सकते हैं परन्तु different name से जैसा की ऊपर बताया गया है।
जब हम arrow operator से किसी structure के members को access करते हैं, तो हमें structure -variable को एक pointer type variable बनाना होता है, यानी आप structure के members access करने के लिए pointer को प्रयोग में ला रहे हैं। इसलिए, एक structure , हम आमतौर पर dot operator का प्रयोग किया जाता है।
syntax
structure-variable.member-name;
जहाँ . एक dot operator है।
इसका program नीचे दिया गया है –
structure program in C++
#include<iostream.h>
#include<conio.h>
#include<stdio.h> // for gets()function
void 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;
getch();
}
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++
जैसा कि हम जानते हैं कि structure – name एक optional होता है लेकिन जब हम structure को initialize करते हैं, तो structure -name अनिवार्य हो जाता है।
structure का initialization साधारण variable से थोड़ा अलग होता है। इसमें सबसे पहले structure member declare किये जाते हैं जो कि bracket के अंदर होते है। फिर bracket के बाहर structure variable declare होता है।
इसी structure -variable से ही structure member उसी क्रम में initialize होते हैं। जिस क्रम में उन्हें declare किया गया हो।
इसका syntax नीचे दिया गया है-
structure-name structure_variable = { members-list } vr;
यहाँ vr एक structure – variable को दर्शाता है, एक structure के एक से अधिक structure -variable हो सकते हैं।
Example
चलिए इसे उदाहरण के साथ समझते हैं ,
सबसे पहले, structure को declare किया जाता है,
struct book = {
int sr,
char name[20],
float price
} vr;
फिर structure-name और structure -variable से उसी क्रम में member को values assign की जाती है-
book vr = {
11,
"tutorial" ,
230.50
};
और जैसा कि हम जानते हैं इस structure के members को structure -variable से access किया जाता है-
vr.sr; // 11
vr.name; //tutorial
vr.price;//230.50
इसको नीचे program में दिया गया है –
#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
previous- array in C++ in hindi
next- union in C++ in hindi