C के Structure और Functions
In this page:
return_type function_name(struct StructName s); // by value (copy)
return_type function_name(struct StructName *s); // by pointer
function_name(variable_name);
function_name(&variable_name);
Structures को Value से Pass करना
यह दिखाता है कि C में default से कोई भी variable कैसे value से pass होता है -- function को हर member की अपनी independent copy मिलती है, इसलिए यह accidental modification से safe है लेकिन caller को changes report भी नहीं कर सकता।
उदाहरण: Passing Structures by Value
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Student
struct Student { int age; };
void tryChange(struct Student s) {
s.age = 99;
}
// Program execution starts in main()
int main() {
struct Student s = {20};
tryChange(s);
// Print formatted text to the screen
printf("%d", s.age);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Structures को Reference से Pass करना
एक pointer pass करना (जैसे void update(struct Student *s)) हर function call पर potentially बड़ी मात्रा में data copy करने से बचाता है, और एक structure में couple से ज़्यादा छोटे fields होने पर standard approach है।
उदाहरण: Passing Structures by Reference
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Student
struct Student { int age; };
void update(struct Student *s) {
s->age = 21;
}
// Program execution starts in main()
int main() {
struct Student s = {20};
update(&s);
// Print formatted text to the screen
printf("%d", s.age);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Functions में Structures Modify करना
function के अंदर, आप pointer के जरिए fields modify करने के लिए arrow operator (s->name = "Bob") इस्तेमाल करते हैं -- यह सीधे caller द्वारा pass किए गए original structure को बदलता है, pass-by-value approach के उलट।
उदाहरण: Modifying Structures in Functions
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Student
struct Student { char name[20]; };
void rename2(struct Student *s) {
s->name[0] = 'B';
}
// Program execution starts in main()
int main() {
struct Student s = {"Ann"};
rename2(&s);
// Print formatted text to the screen
printf("%s", s.name);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Functions से Structures Return करना
struct Point makePoint(int x, int y) { struct Point p = {x, y}; return p; } जैसा एक function एक पूरी तरह बना structure build करके वापस देता है, जो एक out-parameter की ज़रूरत के बिना complex data construct करने का एक clean तरीका है।
उदाहरण: Returning Structures from Functions
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Point
struct Point { int x, y; };
struct Point makePoint(int x, int y) {
struct Point p = {x, y};
// Send p back to the caller
return p;
}
// Program execution starts in main()
int main() {
struct Point p = makePoint(3, 4);
// Print formatted text to the screen
printf("%d %d", p.x, p.y);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Performance Compare करना
कई members या अंदर बड़े arrays वाले एक structure के लिए, हर function call पर value से copy करना जमा होता जाता है -- real programs profile करने से पता चलता है कि एक structure कुछ छोटे fields से बड़ा होते ही pointer-passing लगभग हमेशा तेज़ होता है।
उदाहरण: Comparing Performance
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Big
struct Big { int data[100]; };
void byRef(struct Big *b) {
b->data[0] = 1;
}
// Program execution starts in main()
int main() {
struct Big b = {0};
byRef(&b);
// Print formatted text to the screen
printf("%d", b.data[0]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Value से pass किए एक struct को modify करना और caller की copy बदलने की उम्मीद करना।
- हर बार एक बड़ा struct value से pass करना, जो इसे पूरा copy करता है और program धीमा करता है।
- एक local struct का pointer return करना, जो function खत्म होने के बाद exist नहीं करता।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: