इंडिविजुअल मेंबर्स पास करना
- प्रत्येक मेंबर को फंक्शन कॉल में अलग-अलग आर्गुमेंट के रूप में पास किया जाता है।
- फंक्शन हेडर में साधारण वेरिएबल्स में रिसीव होते हैं।
प्रोग्राम उदाहरण
#include <stdio.h>
struct student {
int s1, s2, s3;
} s[5];
void addition(int a, int b, int c) {
int i, sum;
for(i = 1; i < 4; i++) {
sum = a + b + c;
printf("Student %d scored total of %d\n", i, sum);
}
}
int main() {
int i;
for(i = 1; i < 4; i++) {
printf("Enter marks for student %d in subject 1 = ", i);
scanf("%d", &s[i].s1);
printf("Enter marks for student %d in subject 2 = ", i);
scanf("%d", &s[i].s2);
printf("Enter marks for student %d in subject 3 = ", i);
scanf("%d", &s[i].s3);
}
// नोट: मूल कोड में सिंटैक्स एरर था, सही कॉल:
addition(s[1].s1, s[1].s2, s[1].s3);
return 0;
}
आउटपुट (संशोधित)
Enter marks for student 1 in subject 1 = 10 Enter marks for student 1 in subject 2 = 20 Enter marks for student 1 in subject 3 = 30 ... Student 1 scored total of 60 Student 2 scored total of 60 Student 3 scored total of 60
महत्वपूर्ण नोट
मूल कोड में फंक्शन कॉल addition(s[].s1, s[].s2, s[].s3) अमान्य सिंटैक्स है। सही तरीका प्रत्येक स्टूडेंट के लिए अलग कॉल करना या ऐरे पास करना है।
तुलना
| पैरामीटर | पूरा स्ट्रक्चर पास करना | इंडिविजुअल मेंबर्स पास करना |
|---|---|---|
| सिंटैक्स | func(struct_var) या func(&struct_var) | func(s.m1, s.m2, ...) |
| फंक्शन पैरामीटर | struct tagname या struct tagname* | साधारण वेरिएबल्स |
| मेमोरी | कॉपी बन सकती है (वैल्यू पास) या रेफरेंस | केवल मान पास होते हैं |
| लचीलापन | पूरा स्ट्रक्चर उपलब्ध | केवल चुने हुए मेंबर्स |