Array of Structures

Array of Structures: Structure का प्रयोग Array के साथ मिलाकर किया जा सकता है। हम जानते हैं कि Structure का प्रयोग Related Data Items के Group को Access करने के लिए होता है। हम Structure के विभिन्न Members को Access करने के लिए Variable Declare करते हैं। जब हमें बहुत सारे Variable किसी प्रोग्राम में Declare करने हों, तो हम इस समस्या से बचने के लिए Array का प्रयोग करते हैं। जैसे यदि किसी कक्षा के सभी Students का नाम व उस Student द्वारा प्राप्त किये गए विभिन्न विषयों में प्राप्त अंकों को Store करना हो, तब Array का प्रयोग करना उपयोगी सिद्ध होता है। जैसे:

struct marks
{
    int sub1;
    int sub2;
    int sub3;
};

main()
{
    static struct marks Student[4] = {{41,12,22}, {52,55,66}, {32,55,68}, {55,44,88}};
}

यदि हमें चार Students के तीन विषयों में प्राप्त अंको को Store करना हो, तो Variable को हमें Array प्रकार का Declare करना ठीक रहता है। हमने यहां प्रारम्भिक मान प्रदान किया है। यदि हम चाहें तो Loop द्वारा जिस प्रकार पिछले अध्‍यायों में मान Input किया है, उसी प्रकार यहां भी मान Run Time में Input कर सकते हैं। इस प्रारूप में Stored मानों को हम निम्नानुसार Show कर सकते हैं:

// Output
    Student0.sub1	41
    Student0.sub2	12
    Student0.sub3	22
    Student1.sub1	52
    Student1.sub2	55
    Student1.sub3	66
    Student2.sub1	32
    Student2.sub2	55
    Student2.sub3	68
    Student3.sub1	55
    Student3.sub2	44
    Student3.sub3	88

अब हम इसी प्रोग्राम में Data, Run time में Input करेंगे। इस प्रोग्राम में Data, Run Time में Input करने के लिए प्रोग्राम निम्नानुसार बनेगा-

// Program
struct marks
{
 	int sub1;
 	int sub2;
 	int sub3;
};

struct marks stud[4];
int k;
clrscr();

for( k = 0; k < 4; k++)
{
 	printf("\n Enter marks of Student %d ", k+1);
 	printf("\n Enter Mark of sub1 ");
 	scanf("%d", &stud[k].sub1);
 	printf("\n Enter Mark of sub2 ");
 	scanf("%d", &stud[k].sub2);
 	printf("\n Enter Mark of sub3 ");
	scanf("%d", &stud[k].sub3);
}

clrscr();

for( k = 0; k < 4; k++)
{
	printf("\n Student %d ",k+1 );
	printf("\t\t Mark of sub1 is %d ",stud[k].sub1);
	printf("\n\t\t\t Mark of sub2 is %d ",stud[k].sub2);
	printf("\n\t\t\t Mark of sub3 is %d ",stud[k].sub3);
}

getch();

जिस प्रकार से पहले के प्रोग्रामों में Array का प्रयोग करके मान Input कराया गया है, उसी प्रकार इस प्रोग्राम में भी Array द्वारा मान Input कराया है। सर्वप्रथम एक marks नाम का Structure बनाया है। फिर struct marks प्रकार के Data Type का एक Array Variable stud[10] Declare किया है। ये Array 10 Students के तीन विषयों के Marks Accept करता है। Loop द्वारा एक के बाद एक Student के marks Input करवा, गए हैं। Loop चलाने के लिए एक Variable k int प्रकार का Declare किया है।

जब प्रथम बार Loop Iterate होता है तब “Enter marks of Student1” Message  आता है। चूंकि प्रथम बार में Loop के Variable k का मान 0 होता है इसलिए k + 1 द्वारा यहां 1 print करवाया गया है।

दूसरे Iteration में k का मान 1 होगा तब फिर से “Enter marks of Student2” Message आता है, क्योंकि वापस k + 1 Expression के कारण यहां 2 Print होता है।

हम प्रथम बार जब मान Input करते हैं तब प्रथम विषय में मान जाए, इसे बताने के लिए stud[k].sub1 Expression दिया है, जो Compiler को बताता है कि stud एक Structure प्रकार के Data Type का Variable है और जो मान Input हो रहा है, वह मान sub1 में जाकर Store होगा।

इसी तरह से जब हम दूसरे विषय का मान Input करते हैं, तब stud[k].sub2 Expression  के कारण Compiler समझ जाता है कि अब जो मान Input हुआ है, उसे इस Structure के दूसरे विषय में Store करना है। तीसरे Expression stud[k].sub3 से Input होने वाला मान Structure के तीसरे Member मे जा कर Store होता है।

जब प्रथम Student के तीनों विषय के मान Input हो जाते हैं, तब Program Control Loop से बाहर आकर वापस Loop को Check करता है। Loop की Condition अभी सत्‍य रहती है इसलिए Program Control वापस Loop में चला जाता है। अब k का मान Increment होकर 1 हो चुका है। इसलिए अब Input होने वाले मान दूसरे Student के Memory Location पर जा कर Store होते हैं, क्योंकि k का Index Number बदल जाने से Variable भी बदल जाता है।

हमने Array Chapter के अंतर्गत बताया था कि किस प्रकार से जब हम एक Array Variable Declare करते हैं, तो वह Array वास्तव में उसी समय अलग-अलग Index Number के साथ एक ही Variable के उतने Variable Declare करता है, जितनी हम उसकी Size Define करते हैं। यही बात यहां भी सत्‍य है, इसी कारण से stud[0] व stud[1] Structure marks प्रकार के भिन्न-भिन्न Variables हैं। इसीलिए दूसरे Iteration में k का मान 1 हो जाने से जो मान Input होंगे वे एक अलग Memory Location पर जा कर Store होंगे। इस प्रकार से हम Array का प्रयोग Structure के साथ कर सकते हैं।

Input किये गए मानों को वापस प्राप्त करने के लिए वापस for Loop का प्रयोग किया गया है, और जिन अलग-अलग Memory Locations पर Input किये गए मान Store हुए हैं, वहीं से वापस उन मानों को प्राप्त करके Output में Print करवा दिया गया है। इस प्रोग्राम मे जो मान Input हुए हैं, वे मान एक तरह से Two Dimensional Array के रूप में Store होते हैं।

Buy this eBook for Complete Discussion about “Array of Structure in C

Structure Initialization
Array in Structure

C Programming Language in Hindi - BccFalna.com ये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook  C Programming Language in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी। 

C Programming Language in Hindi | Page: 477 + 265 | Format: PDF

BUY NOW GET DEMO REVIEWS

MidRangeHub Finance Auto Loans Bank Business Health Insurance Insurance Investment Law Lif Insurance Loan Stocks how to endorse a check chase sapphire travel insurance chase sapphire travel delay when are property taxes due Tower Loans how to sell stocks on cash app Voided Check Examples Personal Finance Books Collateral Loans how to sell stocks on cashapp how do you sell your stocks on cash app how to sell stock on cash app joint account sofi joint account ally joint account capital one joint account best bank for joint account chase joint account cyber insurance coverage silverfort free cyber insurance coverage silverfort monjouro savings card Money6x Real Estate