स्ट्रक्चर विभिन्न डेटाटाइप वेरिएबल्स का एक संग्रह है, जिसे एक ही नाम के तहत समूहित किया गया है।
स्ट्रक्चर की विशेषताएं
C प्रोग्रामिंग भाषा में स्ट्रक्चर की विशेषताएं निम्नलिखित हैं:
असाइनमेंट ऑपरेटर का उपयोग करके विभिन्न डेटाटाइप्स के सभी स्ट्रक्चर तत्वों की सामग्री को उसके प्रकार के दूसरे स्ट्रक्चर वेरिएबल में कॉपी करना संभव है।
जटिल डेटाटाइप्स को संभालने के लिए, एक स्ट्रक्चर के भीतर दूसरा स्ट्रक्चर बनाना बेहतर होता है, जिसे नेस्टेड स्ट्रक्चर कहा जाता है।
पूरे स्ट्रक्चर, स्ट्रक्चर के व्यक्तिगत तत्वों और स्ट्रक्चर के पते को फंक्शन में पास करना संभव है।
स्ट्रक्चर पॉइंटर्स बनाना संभव है।
स्ट्रक्चर की घोषणा और इनिशियलाइज़ेशन।
स्ट्रक्चर घोषणा का सामान्य रूप निम्नानुसार है:
datatype member1; struct tagname{ datatype member2; datatype member n; };यहां,
- struct कीवर्ड है।
- tagname स्ट्रक्चर का नाम निर्दिष्ट करता है।
- member1, member2 डेटा आइटम हैं।
उदाहरण के लिए,
struct book{ int pages; char author [30]; float price; };प्रोग्राम
निम्नलिखित C प्रोग्राम स्ट्रक्चर का उपयोग करके नामों को वर्णमाला क्रम में सॉर्ट करने के लिए है:
#include<stdio.h> #include<string.h> struct tag{ char name[10]; int rno; }; typedef struct tag node; node s[5]; sort(int no){ int i,j; node temp; for(i=0;i<no-1;i++) for(j=i+1;j<no;j++) if(strcmp(s[i].name,s[j].name)>0){ temp=s[i]; s[i]=s[j]; s[j]=temp; } } void main(){ int no,i; fflush(stdin); printf("Enter The Number Of Students:"); scanf("%d",&no); for(i=0;i<no;i++){ printf("Enter The Name:"); fflush(stdin); gets(s[i].name); printf("Enter the Roll:"); scanf("%d",&s[i].rno); } sort(no); for(i=0;i<no;i++){ printf("%s\t",s[i].name); printf("%d\n",s[i].rno); } }आउटपुट
जब उपरोक्त प्रोग्राम निष्पादित होता है, तो यह निम्नलिखित परिणाम उत्पन्न करता है:
Enter The Number of Students:5 Enter The Name:Priya Enter the Roll:3 Enter The Name:Hari Enter the Roll:5 Enter The Name:Pinky Enter the Roll:7 Enter The Name:Lucky Enter the Roll:1 Enter The Name:Krishna Enter the Roll:2 Hari 5 Krishna 2 Lucky 1 Pinky 7 Priya 3