सी प्रोग्रामिंग में संरचना की एक सरणी एक ही नाम के तहत समूहीकृत विभिन्न डेटाटाइप चर का संग्रह है।
संरचना घोषणा का सामान्य रूप
संरचनात्मक घोषणा इस प्रकार है -
struct tagname{
datatype member1;
datatype member2;
datatype member n;
}; यहां, संरचना कीवर्ड है।
टैगनाम संरचना का नाम निर्दिष्ट करता है।
सदस्य1, सदस्य2 डेटा आइटम निर्दिष्ट करता है जो संरचना बनाते हैं।
उदाहरण
निम्नलिखित उदाहरण सी प्रोग्रामिंग में एक संरचना के भीतर सरणी के उपयोग को दर्शाता है -
struct book{
int pages;
char author [30];
float price;
}; उदाहरण
एक संरचना के भीतर एक सरणी के उपयोग को प्रदर्शित करने के लिए सी प्रोग्राम निम्नलिखित है -
#include <stdio.h>
// Declaration of the structure candidate
struct candidate {
int roll_no;
char grade;
// Array within the structure
float marks[4];
};
// Function to displays the content of
// the structure variables
void display(struct candidate a1){
printf("Roll number : %d\n", a1.roll_no);
printf("Grade : %c\n", a1.grade);
printf("Marks secured:\n");
int i;
int len = sizeof(a1.marks) / sizeof(float);
// Accessing the contents of the
// array within the structure
for (i = 0; i < len; i++) {
printf("Subject %d : %.2f\n",
i + 1, a1.marks[i]);
}
}
// Driver Code
int main(){
// Initialize a structure
struct candidate A= { 1, 'A', { 98.5, 77, 89, 78.5 } };
// Function to display structure
display(A);
return 0;
} आउटपुट
जब उपरोक्त प्रोग्राम को निष्पादित किया जाता है, तो यह निम्नलिखित परिणाम उत्पन्न करता है -
Roll number : 1 Grade : A Marks secured: Subject 1 : 98.50 Subject 2 : 77.00 Subject 3 : 89.00 Subject 4 : 78.50