← Back to C Course | Chapter 7: Pointers | Lesson 5 of 9

C के Pointers और Functions

Functions को pointers pass करना उन्हें original data बदलने देता है, एक copy की बजाय एक address सौंपने जैसा। इसी तरह एक function आपके दो variables को swap कर सकता है।
Syntax
c
void function_name(data_type param) { }    // call by value
void function_name(data_type *param) {     // call by reference
  *param = new_value;
}

function_name(&variable_name);

Call by Value

हर non-array, non-pointer types के लिए यह C का default parameter-passing behavior है -- यह caller के data को protect करता है लेकिन इसका मतलब है function के पास अपने return value के अलावा result report करने का कोई तरीका नहीं, जो इसे एक single output तक सीमित करता है।

उदाहरण: Call by Value

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void tryChange(int x) {
	x = 100;
}
// Program execution starts in main()
int main() {
	// Declare value and set it to 5
	int value = 5;
	tryChange(value);
	// Print formatted text to the screen
	printf("%d", value);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Call by Reference

यह standard technique है जो C 'output parameters' simulate करने के लिए इस्तेमाल करता है, क्योंकि एक function वाकई सिर्फ एक value return कर सकता है -- scanf() जैसे functions पूरी तरह इस pattern पर depend करते हैं, यही कारण है कि आप address-of operator के साथ scanf("%d", &x) लिखते हैं।

उदाहरण: Call by Reference

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void change(int *x) {
	*x = 100;
}
// Program execution starts in main()
int main() {
	// Declare value and set it to 5
	int value = 5;
	change(&value);
	// Print formatted text to the screen
	printf("%d", value);
	// Return 0 to signal that the program finished successfully
	return 0;
}

दो Numbers Swap करना

एक classic teaching example जो दिखाता है कि क्यों अकेला call by value दो variables swap नहीं कर सकता: function को दोनों variables के pointers मिलने चाहिए ताकि यह dereference करके उनकी actual stored values exchange कर सके, सिर्फ local copies नहीं।

उदाहरण: Swapping Two Numbers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void swap(int *a, int *b) {
	// Declare temp and set it to *a
	int temp = *a;
	*a = *b;
	*b = temp;
}
// Program execution starts in main()
int main() {
	int x = 5, y = 10;
	swap(&x, &y);
	// Print formatted text to the screen
	printf("%d %d", x, y);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Functions से Pointers Return करना

यह genuinely dangerous pattern है जिससे बचना चाहिए -- एक local variable की memory function के return होते ही reuse हो जाती है, इसलिए इसका एक pointer एक 'dangling pointer' बन जाता है जो invalid, potentially overwritten memory की ओर point करता है।

उदाहरण: Returning Pointers from Functions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int* getStatic() {
	static int value = 42;
	// Send &value back to the caller
	return &value;
}
// Program execution starts in main()
int main() {
	int *ptr = getStatic();
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pointer Arrays Pass करना

Pointers का एक array pass करना (जैसे char *names[]) एक single function को separately-allocated strings या structures के एक पूरे collection पर काम करने देता है इसके बिना कि उसे पहले से उनके individual sizes पता हों -- main() में argv exactly ऐसे ही काम करता है।

उदाहरण: Passing Pointer Arrays

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void printNames(char *names[], int count) {
	// Loop: repeat while the condition holds
	for (int i = 0; i < count; i++) {
		// Print formatted text to the screen
		printf("%s ", names[i]);
	}
}
// Program execution starts in main()
int main() {
	char *names[2] = {"Ann", "Bob"};
	printNames(names, 2);
	// 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. यह उम्मीद करना कि एक function caller का variable बदल देगा जब इसे value से एक copy मिली थी, जैसे एक swap जो सिर्फ copies swap करता है।
  2. call site पर & भूल जाना जब function को एक pointer चाहिए, ताकि गलत value pass हो।
  3. एक local variable का pointer return करना, जो function के return होने पर destroy हो जाता है।

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.