🟡 Intermediate  ·  Lesson 26

Pointers और Functions

Functions में Pointer

Pointers की मदद से function original variables के address पर काम कर सकता है। इससे values को directly modify किया जा सकता है।

Call by Value

Call by value में value की copy जाती है। Function के अंदर change original variable को affect नहीं करता।

C Language
void change(int x) {
    x = 100;
}

Call by Address

Call by address में variable का address pass होता है। Function pointer parameter के द्वारा original value change करता है।

C Language
#include <stdio.h>

void swap(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}

int main() {
    int a = 10, b = 20;
    swap(&a, &b);
    printf("%d %d", a, b);
    return 0;
}
20 10

Array Pointer Function

C Language
int sumArray(int *arr, int n) {
    int sum = 0;
    for(int i = 0; i < n; i++)
        sum += arr[i];
    return sum;
}

Pointer Return करना

⚠️ सावधानी

Local variable का address return न करें, क्योंकि function खत्म होते ही local variable destroy हो जाता है।

सारांश

  • Call by value में copy pass होती है
  • Call by address में address pass होता है
  • Original value change करने के लिए pointer parameter use करें
  • Variable का address भेजने के लिए & use करें
  • Pointer functions multiple values update करने में useful हैं

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C में function को pointer कैसे भेजते हैं?
आप function parameter को pointer के रूप में declare करते हैं, जैसे void update(int *p), और उसे किसी variable के address से call करते हैं, जैसे update(&x)। Function के अंदर आप *p से मूल variable को सीधे पढ़ते या बदलते हैं।
C में call by address क्या है?
Call by address (call by reference भी कहा जाता है) का मतलब function को variable की value के बजाय उसका address भेजना है। चूँकि function को address मिलता है, pointer के ज़रिए किए बदलाव caller के मूल variable को प्रभावित करते हैं, call by value के विपरीत जो copy पर काम करता है।
Function arguments में pointers क्यों इस्तेमाल करें?
Pointers function को caller के variables बदलने, एक से ज़्यादा परिणाम लौटाने, और arrays या structures जैसे बड़े data की copy से बचने देते हैं। Address भेजना सस्ता है और function को copy के बजाय असली data पर काम करने देता है।
क्या C में function pointer लौटा सकता है?
हाँ, function pointer लौटा सकता है, पर उसे किसी local variable का address नहीं लौटाना चाहिए, क्योंकि वह memory function खत्म होने पर free हो जाती है। malloc से मिली memory या function से आगे जीने वाली चीज़ का pointer लौटाना सुरक्षित है।
call by value और call by address में क्या अंतर है?
Call by value copy भेजता है, तो function caller का variable नहीं बदल सकता। Call by address variable का address भेजता है, तो function pointer के ज़रिए मूल बदल सकता है। जब बदलाव टिकने चाहिए तब call by address इस्तेमाल करें।
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.