← Back to C Course | Chapter 8: Structures & Unions | Lesson 3 of 7

C के Structure और Functions

एक structure को एक function को pass करना इसे copy करके या इसका address देकर किया जा सकता है, एक photocopy या original paper सौंपने जैसा। Copies original को unchanged छोड़ती हैं।
Syntax
c
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

c
// 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;
}

Structures को Reference से Pass करना

एक pointer pass करना (जैसे void update(struct Student *s)) हर function call पर potentially बड़ी मात्रा में data copy करने से बचाता है, और एक structure में couple से ज़्यादा छोटे fields होने पर standard approach है।

उदाहरण: Passing Structures by Reference

c
// 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;
}

Functions में Structures Modify करना

function के अंदर, आप pointer के जरिए fields modify करने के लिए arrow operator (s->name = "Bob") इस्तेमाल करते हैं -- यह सीधे caller द्वारा pass किए गए original structure को बदलता है, pass-by-value approach के उलट।

उदाहरण: Modifying Structures in Functions

c
// 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;
}

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

c
// 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;
}

Performance Compare करना

कई members या अंदर बड़े arrays वाले एक structure के लिए, हर function call पर value से copy करना जमा होता जाता है -- real programs profile करने से पता चलता है कि एक structure कुछ छोटे fields से बड़ा होते ही pointer-passing लगभग हमेशा तेज़ होता है।

उदाहरण: Comparing Performance

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Value से pass किए एक struct को modify करना और caller की copy बदलने की उम्मीद करना।
  2. हर बार एक बड़ा struct value से pass करना, जो इसे पूरा copy करता है और program धीमा करता है।
  3. एक local struct का pointer return करना, जो function खत्म होने के बाद exist नहीं करता।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.