सी में स्ट्रक्चर क्या है? |स्ट्रक्चर का उपयोग क्यों करें?( structure of c program in hindi)

structure of c program in hindi

स्ट्रक्चर का उपयोग क्यों करें?

सी में, ऐसे मामले हैं जहां हमें एक इकाई के कई गुणों को स्टोर करने की आवश्यकता होती है। यह आवश्यक नहीं है कि किसी संस्था (Institution) के पास केवल एक ही प्रकार की सारी जानकारी हो। इसमें विभिन्न डेटा प्रकारों के विभिन्न गुण हो सकते हैं। उदाहरण के लिए, एक इकाई छात्र का नाम (string), रोल नंबर (int), अंक (float) हो सकता है। एक इकाई छात्र के संबंध में इस प्रकार की जानकारी संग्रहीत करने के लिए, हमारे पास निम्नलिखित दृष्टिकोण  हैं:-

• नामों, रोल नंबरों और चिह्नों को संग्रहीत करने के लिए अलग-अलग सरणियों (arrays) का निर्माण करें।

• विभिन्न डेटा प्रकारों (data structure) के संग्रह को संग्रहीत करने के लिए एक विशेष डेटा संरचना (data types) का उपयोग करें।

आइए पहले दृष्टिकोण को विस्तार से देखें।

#include<stdio.h>  

void main ()  

{  

  char names[2][10],dummy;    // 2- dimensioanal character array names is used to store the names of the students   

    int roll_numbers[2],i;  

      float marks[2];  

        for (i=0;i<3;i++)  

          {  

    printf("Enter the name, roll number, and marks of the student %d",i+1);  

       scanf("%s %d %f",&names[i],&roll_numbers[i],&marks[i]);  

       scanf("%c",&dummy); // enter will be stored into dummy character at each iteration  

        }  

         printf("Printing the Student details ...\n");  

         for (i=0;i<3;i++)  

        {  

        printf("%s %d %f\n",names[i],roll_numbers[i],marks[i]);  

        }  

        } 

Output:-

Enter the name, roll number, and marks of the student 1vemoun 90 84
Enter the name, roll number, and marks of the student 2marthin 67 55
Enter the name, roll number, and marks of the student 3sohail 68 91
Printing the Student details ...
vemoun 90 84.000000
marthin 67 55.000000
sohail 68 91.000000

--------------------------------
Process exited after 88.08 seconds with return value 20
Press any key to continue . . .


उपरोक्त कार्यक्रम (above program) एक इकाई छात्र की जानकारी संग्रहीत करने की हमारी आवश्यकता को पूरा कर सकता है। हालांकि, कार्यक्रम बहुत जटिल है (program is too complicated) और इनपुट की मात्रा के साथ complexity बढ़ जाती है। प्रत्येक सरणी (arrays) के तत्वों (elements) को एक साथ संग्रहीत किया जाता है, लेकिन सभी सरणियों को स्मृति में सन्निहित रूप से संग्रहीत नहीं किया जा सकता है। सी आपको एक अतिरिक्त और सरल दृष्टिकोण प्रदान करता है जहां आप एक विशेष डेटा संरचना, यानी संरचना का उपयोग कर सकते हैं, जिसमें आप एक इकाई के संबंध में विभिन्न डेटा प्रकार की सभी जानकारी समूहित कर सकते हैं।

सी में स्ट्रक्चर क्या है?


सी में Structure एक उपयोगकर्ता-परिभाषित डेटा प्रकार (user-defined data type) है जो हमें विभिन्न डेटा प्रकारों के संग्रह को संग्रहीत करने में सक्षम बनाता है। संरचना के प्रत्येक तत्व को सदस्य कहा जाता है। संरचनाएं ca; कक्षाओं और टेम्प्लेट के उपयोग का अनुकरण करें, क्योंकि यह विभिन्न सूचनाओं को संग्रहीत कर सकता है।
स्ट्रक्चर को परिभाषित करने के लिए स्ट्रक्चर कीवर्ड का इस्तेमाल किया जाता है। आइए सी में संरचना को परिभाषित करने के लिए वाक्यविन्यास देखें।

struct structure_name   

{  

    data_type member1;  

    data_type member2;  

    .  

    .  

    data_type memeberN;  

 };  


आइए सी में एक इकाई कर्मचारी के लिए एक संरचना को परिभाषित करने के लिए उदाहरण देखें।

struct employee  

{   int id;  

 char name[20];  

 float salary;  

 };  


निम्न छवि संरचना कर्मचारी (image structure staff) के स्मृति आवंटन (memory allocation) को दिखाती है जिसे उपरोक्त उदाहरण में परिभाषित किया गया है।


यहां, संरचना कीवर्ड है; कर्मचारी संरचना का नाम है; आईडी, नाम और वेतन संरचना के सदस्य या क्षेत्र हैं। आइए इसे नीचे दिए गए डायग्राम से समझते हैं:-

स्ट्रक्चर वेरिएबल डिक्लेयर करना।

हम संरचना (structure) के लिए एक चर घोषित कर सकते हैं ताकि हम संरचना के सदस्य तक आसानी से पहुंच सकें। स्ट्रक्चर वेरिएबल घोषित करने के दो तरीके हैं।

• main() फ़ंक्शन के भीतर स्ट्रक्चर कीवर्ड द्वारा।

• संरचना को परिभाषित करते समय एक variable घोषित करके।

 पहला तरीका:-
आइए स्ट्रक्चर कीवर्ड द्वारा स्ट्रक्चर वेरिएबल घोषित करने के लिए उदाहरण देखें। इसे मुख्य समारोह के भीतर घोषित किया जाना चाहिए।

struct employee  

{   int id;  

    char name[50];  

    float salary;  

 };  

अब दिए गए कोड को main() फंक्शन के अंदर लिखें।

struct employee e1, e2;  

संरचना में संग्रहीत मूल्यों तक पहुँचने के लिए चर(variables) e1 और e2 का उपयोग किया जा सकता है। यहां, e1 और e2 को उसी तरह से व्यवहार किया जा सकता है जैसे C++ और Java में ऑब्जेक्ट।

दूसरा तरीका:-

आइए संरचना को परिभाषित करते समय वेरिएबल घोषित करने का एक और तरीका देखें।

struct employee  

{   int id;  

    char name[50];  

    float salary;  

     }e1,e2; 

कौन सा तरीका अच्छा है?

यदि  वेरिएबलियो की संख्या निश्चित नहीं है, तो पहले दृष्टिकोण का उपयोग करें। यह आपको कई बार संरचना वेरिएबल घोषित करने की सुविधा प्रदान करता है।

अगर नहीं। वेरिएबल निश्चित हैं, दूसरे दृष्टिकोण का उपयोग करें। यह main() फ़ंक्शन में एक चर घोषित करने के लिए आपके कोड को सहेजता है।

स्ट्रक्चर के सदस्यों तक पहुंचना।

संरचना सदस्यों तक पहुँचने के दो तरीके हैं।

1.By . (member or dot operator)

2.By -> (structure pointer operator)

आइए, p1 वेरिएबल के id सदस्य को एक्सेस करने के लिए कोड देखें। (सदस्य) ऑपरेटर।

p1.id

C Structure example:-

#include<stdio.h>  

#include <string.h>    

struct employee      

{   int id;      

    char name[50];      

    }e1;  //declaring e1 variable for structure    

    int main( )    

    {    

       //store first employee information    

          e1.id=101;    

          strcpy(e1.name, "Richa Suri");//copying string into char array    

            //printing first employee information    

            printf( "employee 1 id : %d\n", e1.id);    

            printf( "employee 1 name : %s\n", e1.name);    

            return 0;  

     }    

Output:-

employee 1 id : 101
employee 1 name : Richa Suri

--------------------------------
Process exited after 0.02761 seconds with return value 0
Press any key to continue . . .

आइए कई कर्मचारियों की जानकारी संग्रहीत करने के लिए सी भाषा में संरचना का एक और उदाहरण देखें।

#include<stdio.h>  

#include <string.h>    

struct employee      

{   int id;      

    char name[50];      

        float salary;      

        }e1,e2;  //declaring e1 and e2 variables for structure    

        int main( )    

        {    

           //store first employee information    

              e1.id=101;    

              strcpy(e1.name, "Sohail khan");//copying string into char array    

                    e1.salary=56000;    

  //store second employee information    

     e2.id=102;    

        strcpy(e2.name, "James Bond");    

           e2.salary=126000;    

   //printing first employee information    

      printf( "employee 1 id : %d\n", e1.id);    

         printf( "employee 1 name : %s\n", e1.name);    

            printf( "employee 1 salary : %f\n", e1.salary);    

   //printing second employee information    

      printf( "employee 2 id : %d\n", e2.id);    

         printf( "employee 2 name : %s\n", e2.name);    

            printf( "employee 2 salary : %f\n", e2.salary);    

               return 0;    

               }  

Output:-

employee 1 id : 101
employee 1 name : Sohail khan
employee 1 salary : 56000.000000
employee 2 id : 102
employee 2 name : James Bond
employee 2 salary : 126000.000000

--------------------------------
Process exited after 0.03004 seconds with return value 0
Press any key to continue . . .

Previous articleImport module in Python with Examples (Modules of Python)
Next articlewrite a program to print the Fibonacci series in c. |Fibonacci series program in c.

LEAVE A REPLY

Please enter your comment!
Please enter your name here