Notes in Hindi

Passing Pointer to Function in C in Hindi

/ BCA / Programming with C and CPP

Pointer Function Usage in C Explained in Hindi

Passing Pointer to Function in C in Hindi

What is Pointer Passing?

जब हम किसी function को एक variable का address भेजते हैं, तो इसे "Passing Pointer to Function" कहते हैं। इसका मतलब है कि हम variable की actual memory को access करने की अनुमति function को दे रहे हैं।

Why use Pointer in Function?

  • Memory की efficiency बढ़ाने के लिए
  • Function के अंदर value को directly modify करने के लिए
  • Large data structures (जैसे arrays) को efficiently पास करने के लिए

Basic Syntax

#include <stdio.h> void update(int *p) { *p = *p + 10; } int main() { int a = 5; update(&a); printf("Updated value: %d", a); return 0; }

Explanation in Hindi

ऊपर दिए गए उदाहरण में, हमने एक integer a को define किया और उसका address &a function update() को पास किया। function ने pointer के द्वारा उस address की value को modify किया।

Important Points

  • Pointer को use करने से function original variable की value बदल सकता है।
  • Pointer की मदद से memory sharing possible होती है।

Returning Pointer from Function in Hindi

Function se Pointer Return Karna क्या होता है?

जब कोई function execution के बाद एक pointer return करता है, तो इसका मतलब है वह function memory address वापस कर रहा है, जिससे हम future में data access कर सकते हैं।

Return Pointer के Uses

  • Dynamic memory allocation से created memory को access करने के लिए
  • String या Array को return करने के लिए

Code Example

#include <stdio.h> #include <stdlib.h> int* createArray() { int *arr = (int*)malloc(3 * sizeof(int)); arr[0] = 10; arr[1] = 20; arr[2] = 30; return arr; } int main() { int *ptr = createArray(); for (int i = 0; i < 3; i++) { printf("%d ", ptr[i]); } free(ptr); return 0; }

Explanation

इस code में createArray() function dynamic memory allocate करता है और उसका address return करता है। इसे main function में pointer से receive किया गया और उस memory को access किया गया।

Important Notes

  • Static या local variable का address return न करें क्योंकि उनकी memory function के बाद destroy हो जाती है।
  • Heap memory को return किया जा सकता है क्योंकि वह function खत्म होने के बाद भी रहती है।

Call by Reference using Pointer in Hindi

Call by Reference क्या है?

Call by Reference का मतलब है variable के address को function को pass करना, जिससे original value को modify किया जा सके। C language में इसे pointer के द्वारा achieve किया जाता है।

Code Example

#include <stdio.h> void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } int main() { int a = 5, b = 10; swap(&a, &b); printf("a = %d, b = %d", a, b); return 0; }

Explanation

swap() function को a और b के address भेजे गए हैं। function ने pointers का उपयोग करके original variables की values को बदल दिया।

फायदे

  • Original value को modify करना संभव
  • Memory efficient

Difference Between Call by Value and Call by Reference

Call by Value Call by Reference
Original value नहीं बदलती Original value बदल सकती है
Value की copy पास होती है Address पास होता है
Less memory efficient More memory efficient

Pointer as Function Argument in Hindi

Pointer को Argument के रूप में क्यों उपयोग करते हैं?

Function के arguments में pointer use करने से function को अधिक power मिलती है कि वह original variable को access और modify कर सके।

Example

#include <stdio.h> void doubleValue(int *num) { *num = *num * 2; } int main() { int x = 7; doubleValue(&x); printf("Doubled value = %d", x); return 0; }

Working समझिए

&x का मतलब है x का address, जो function को भेजा गया। function ने उस address पर stored value को double कर दिया।

Pointer Argument के Benefits

  • Function की efficiency बढ़ती है
  • Original data को change करना आसान होता है
  • Array, Structure, और Dynamic Memory से काम आसान होता है

Pointer vs Non-Pointer Argument

Pointer Argument Non-Pointer Argument
Original value को modify कर सकते हैं केवल copy change होती है
Efficient in memory usage Less efficient
Address पर काम करता है Value पर काम करता है

Final Summary (सारांश)

  • Pointer को function में pass करना बहुत उपयोगी है जब original variable को modify करना हो।
  • Pointer को function से return करना तब उपयोगी होता है जब dynamically allocated memory या array वापस करना हो।
  • Call by Reference में हम address pass करते हैं जिससे original value बदल सकती है।
  • Pointer को argument के रूप में उपयोग करने से memory efficiency बढ़ती है और large data structures को easily manage किया जा सकता है।

FAQs

जब हम किसी function को किसी variable का address भेजते हैं, ताकि function उस variable की value को सीधे modify कर सके, उसे "Passing Pointer to Function" कहते हैं। इससे memory efficient तरीके से value को change किया जा सकता है।
Function से Pointer return करने के लिए हमें किसी dynamic memory (जैसे malloc से allocate की गई memory) का address return करना चाहिए। Local variable का address return करना गलत होता है क्योंकि वो function के खत्म होने के बाद destroy हो जाता है।
Call by Reference में हम variable का address function को भेजते हैं, जिससे function उसी memory location को access करके value को directly modify कर सकता है। C में यह pointer की मदद से किया जाता है।
Pointer को argument के रूप में use करने से हम memory efficient तरीके से काम कर सकते हैं, original data को modify कर सकते हैं और large data structures (जैसे array या structure) को आसानी से pass कर सकते हैं।
नहीं, local variable का address return करना गलत होता है क्योंकि जैसे ही function खत्म होता है, उस local variable की memory release हो जाती है और return किया गया pointer invalid हो जाता है। हमें heap से allocate की गई memory का address return करना चाहिए।

Please Give Us Feedback