C के Pointers और Functions
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
दो Numbers Swap करना
एक classic teaching example जो दिखाता है कि क्यों अकेला call by value दो variables swap नहीं कर सकता: function को दोनों variables के pointers मिलने चाहिए ताकि यह dereference करके उनकी actual stored values exchange कर सके, सिर्फ local copies नहीं।
उदाहरण: Swapping Two Numbers
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Functions से Pointers Return करना
यह genuinely dangerous pattern है जिससे बचना चाहिए -- एक local variable की memory function के return होते ही reuse हो जाती है, इसलिए इसका एक pointer एक 'dangling pointer' बन जाता है जो invalid, potentially overwritten memory की ओर point करता है।
उदाहरण: Returning Pointers from Functions
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Pointer Arrays Pass करना
Pointers का एक array pass करना (जैसे char *names[]) एक single function को separately-allocated strings या structures के एक पूरे collection पर काम करने देता है इसके बिना कि उसे पहले से उनके individual sizes पता हों -- main() में argv exactly ऐसे ही काम करता है।
उदाहरण: Passing Pointer Arrays
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि एक function caller का variable बदल देगा जब इसे value से एक copy मिली थी, जैसे एक swap जो सिर्फ copies swap करता है।
- call site पर
&भूल जाना जब function को एक pointer चाहिए, ताकि गलत value pass हो। - एक local variable का pointer return करना, जो function के return होने पर destroy हो जाता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: