PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Parameter Passing: Call by Reference (Using Pointers) (कॉल बाई रेफरेंस: पते (Address) पास करना और चरों में बदलाव) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Call by Reference in C is implemented using pointers by passing variable memory addresses (&var) into formal pointer parameters (*ptr), allowing direct modification of caller data via dereferencing. It enables swapping, multiple output values, and zero-copy passing of large structs, while demanding strict NULL and lvalue safety.
अध्याय सार: C में कॉल बाई रेफरेंस पॉइंटर्स के माध्यम से लागू किया जाता है, जहाँ कॉल करते समय वेरिएबल्स का मेमोरी पता (&var) फॉर्मल पॉइंटर पैरामीटर (*ptr) में भेजा जाता है। डीरेफरेंस (*) द्वारा फंक्शन सीधे मूल मेमोरी में परिवर्तन कर सकता है, जिससे स्वैपिंग, कई आउटपुट लौटाना और बड़े स्ट्रक्चर को बिना कॉपी पास करना संभव होता है।
Parameter Passing: Call by Reference (Using Pointers): Detailed Notes & Architecture (कॉल बाई रेफरेंस: पते (Address) पास करना और चरों में बदलाव)
1. Pointer-Based Call by Reference Mechanics (& and *) (पॉइंटर द्वारा कॉल बाई रेफरेंस की कार्यप्रणाली)
[EN] ISO C lacks C++'s implicit reference syntax (int &r) and instead achieves Call by Reference through explicit pointers. At the call site, the caller passes variable addresses using the address-of operator '&x'; the callee receives those addresses inside formal pointer parameters like 'int *p'. Inside the callee, dereferencing via '*p = 50;' accesses and overwrites the caller's actual memory location directly, persisting after the function returns.
[HI] C में C++ जैसा सीधा रेफरेंस सिंटैक्स (int &r) नहीं होता, बल्कि '&' ऑपरेटर से वेरिएबल का मेमोरी पता भेजकर उसे 'int *p' पॉइंटर पैरामीटर में प्राप्त किया जाता है। फंक्शन के अंदर डीरेफरेंस ऑपरेटर (*p = 50;) सीधे कॉलर के मूल वेरिएबल की मेमोरी लोकेशन पर मान लिख देता है, जिससे फंक्शन लौटने के बाद भी बदलाव स्थायी रहता है।
2. Variable Swapping & Multiple Output Parameters (स्वैपिंग तथा एक से अधिक आउटपुट मान लौटाना)
[EN] Because a C return statement yields at most one value, pointer-based Call by Reference is essential whenever a function must modify caller variables or return multiple results—such as swap(&x, &y), divMod(a, b, ", &rem), or scanf("%d", &num). Inside swap(int *a, int *b), dereferencing *a and *b exchanges the underlying caller integers in place across stack frames.
[HI] C का return स्टेटमेंट एक बार में केवल एक ही मान लौटा सकता है; अतः स्वैपिंग (swap(&x, &y)), भागफल व शेषफल एक साथ लौटाने (divMod) या scanf("%d", &num) द्वारा इनपुट भरने के लिए पॉइंटर आधारित कॉल बाई रेफरेंस ही मुख्य उपाय है। swap(int *a, int *b) में *a व *b की अदला-बदली सीधे मुख्य प्रोग्राम के मान बदल देती है।
3. Zero-Copy Struct Efficiency & const Pointer Guards (शून्य-कॉपी दक्षता व const पॉइंटर सुरक्षा)
[EN] Passing a large structure via pointer ('struct Student *s') pushes only one 4-byte or 8-byte memory address onto the stack instead of duplicating hundreds of bytes. When the callee only needs read-only access, adding const ('const struct Student *s') retains zero-copy efficiency while instructing the compiler to forbid any accidental mutation through '*s'. Note the difference between 'const int *p' (read-only target) and 'int * const p' (constant pointer address).
[HI] बड़े स्ट्रक्चर को पॉइंटर ('struct Student *s') से भेजने पर सैकड़ों बाइट्स की कॉपी के बजाय केवल 4 या 8 बाइट का पता ही स्टैक पर जाता है। यदि फंक्शन को केवल डेटा पढ़ना हो, तो 'const struct Student *s' लिखकर बिना कॉपी किए कंपाइलर स्तर पर अनचाहे बदलाव को भी रोका जा सकता है।
4. Safety Hazards: NULL Pointers, Aliasing & C++ Contrast (पॉइंटर जोखिम, एलियासिंग व C++ रेफरेंस से अंतर)
[EN] Unlike C++ references—which must bind to valid objects, cannot be NULL, and dereference automatically—C pointer parameters can receive NULL or dangling addresses and require explicit '*' syntax. Production C functions validate 'if (p != NULL)' before dereferencing to avoid segmentation faults and never pass constants like 'swap(&5, &10)' because literals lack addressable memory. Beware pointer aliasing too: passing the same address twice like 'add(&a, &a)' causes both pointers to mutate the same variable simultaneously.
[HI] C++ रेफरेंस (जो NULL नहीं हो सकते और स्वतः डीरेफरेंस होते हैं) के विपरीत C पॉइंटर्स में NULL या डैंगलिंग एड्रेस आने का खतरा रहता है, अतः डीरेफरेंस से पहले 'if (p != NULL)' जांच करना सुरक्षित रहता है। स्थिरांकों का पता (&5) पास नहीं किया जा सकता क्योंकि उनकी कोई एल्वैल्यू मेमोरी नहीं होती। साथ ही दो बार एक ही पता (&a, &a) भेजने पर पॉइंटर एलियासिंग से अनपेक्षित मान आ सकते हैं।
Syntax, Formula & Technical Example (Parameter Passing: Call by Reference (Using Pointers))
#include <stdio.h>
void swap(int *a, int *b) { // Formal pointers receive addresses
int temp = *a; *a = *b; *b = temp; // Mutates caller memory
}
int main(void) {
int x = 10, y = 25;
swap(&x, &y); // Pass addresses via &
printf("x=%d, y=%d\n", x, y); // Output: x=25, y=10
return 0;
}
Parameter Passing: Call by Reference (Using Pointers) Quick Reference & Comparison Table
| Aspect | Call by Reference (C Pointers) | Contrast / Exam Comparison |
|---|---|---|
| Call & Header Syntax | Pass &x into formal pointer int *p | Call by Value passes x directly into int p |
| Caller Mutation Effect | Writing *p modifies caller memory permanently | Enables swaps and multiple output arguments |
| Struct Efficiency | Copies only 4/8 byte address onto stack | Use const T* for zero-copy read-only safety |
| C Pointer vs C++ Ref | C pointers may be NULL and use * and & | C++ int &r cannot be NULL and needs no * |
Parameter Passing: Call by Reference (Using Pointers) Top 20 Bilingual MCQs with Answers
Q1. How is Call by Reference implemented in standard ISO C?
(मानक ISO C में कॉल बाई रेफरेंस किस प्रकार लागू किया जाता है?)
(A) By using C++ reference syntax int &x
(B) By passing arrays inside structs
(C) By using the ref keyword in the header
(D) By passing variable addresses (&) into pointer parameters (*) and dereferencing them
Correct Answer: (D)
[EN] C simulates Call by Reference explicitly by passing pointers to variables rather than implicit reference aliases.
[HI] C में कॉल बाई रेफरेंस वेरिएबल्स के पते (&) को पॉइंटर पैरामीटर (*) में पास करके तथा डीरेफरेंस द्वारा लागू होता है।
Q2. Given 'void swap(int *p, int *q)', how must main() call it to swap 'int a = 3, b = 7;'?
('void swap(int *p, int *q)' द्वारा 'int a = 3, b = 7;' को स्वैप करने के लिए main() में सही कॉल क्या होगी?)
(A) swap(*a, *b);
(B) swap(&&a, &&b);
(C) swap(a, b);
(D) swap(&a, &b);
Correct Answer: (D)
[EN] The function expects integer pointers (int *), so caller passes addresses &a and &b.
[HI] फंक्शन int* पॉइंटर लेता है, इसलिए कॉल करते समय पते &a और &b भेजने होंगे।
Q3. Which standard C library function relies on pointer-based Call by Reference to write inputs into caller variables?
(इनमें से कौन सा मानक C लाइब्रेरी फंक्शन कॉलर के वेरिएबल्स में इनपुट भरने के लिए पॉइंटर-आधारित कॉल बाई रेफरेंस उपयोग करता है?)
(A) strlen(s) returning int
(B) scanf("%d %d", &x, &y);
(C) abs(x)
(D) sqrt(x)
Correct Answer: (B)
[EN] scanf takes addresses &x and &y so it can store multiple parsed input values directly into caller variables.
[HI] scanf अपने आर्गुमेंट्स में &x व &y पते लेकर सीधे कॉलर के वेरिएबल्स में इनपुट मान संग्रहीत करता है।
Q4. Trace output: void f(int *p, int *q) { p = q; *p = 20; } int main() { int a=5, b=10; f(&a, &b); printf("%d %d", a, b); }
(कोड 'void f(int *p, int *q) { p = q; *p = 20; }' में f(&a, &b) के बाद a और b का आउटपुट क्या होगा?)
(A) 5 20
(B) 20 20
(C) 20 10
(D) 5 10
Correct Answer: (A)
[EN] Initially p=&a and q=&b; then p = q redirects p to point to b, so *p = 20 updates b to 20 while a remains 5.
[HI] पहले p=&a व q=&b थे, फिर p = q होने से p भी b की ओर इशारा करने लगा, अतः *p = 20 केवल b को 20 करेगा और a 5 रहेगा।
Q5. Trace output: void mystery(int *p, int x) { x = x + 5; *p = *p + x; } int main() { int a=10; mystery(&a, a); printf("%d", a); }
('void mystery(int *p, int x) { x += 5; *p += x; }' को mystery(&a, a) से कॉल करने पर (a=10) क्या प्रिंट होगा?)
(A) 20
(B) 10
(C) 15
(D) 25
Correct Answer: (D)
[EN] x receives a copy 10 and becomes 15; then *p (which points to a) adds 15 to 10, setting a = 25.
[HI] x को कॉपी 10 मिलकर 15 होता है, और फिर *p (अर्थात मूल a) में 15 जुड़कर 10 + 15 = 25 प्रिंट होगा।
Q6. Why does calling 'swap(&5, &10);' cause a compile-time error in C?
('swap(&5, &10);' कॉल करने पर C में कंपाइल-टाइम एरर क्यों आता है?)
(A) Pointers cannot point to odd integers
(B) 5 and 10 cannot fit in int
(C) swap only accepts positive values over 100
(D) Numeric literals 5 and 10 are rvalues without addressable memory locations for '&'
Correct Answer: (D)
[EN] The address-of operator '&' can only be applied to an lvalue residing in modifiable memory, never literal constants.
[HI] स्थिरांक 5 व 10 का कोई मेमोरी पता (Lvalue) नहीं होता, इसलिए उन पर '&' ऑपरेटर लगाना अवैध है।
Q7. Why does 'void inc(int &r) { r++; }' fail under a strict C compiler?
('void inc(int &r) { r++; }' लिखने पर C कंपाइलर एरर क्यों देता है?)
(A) & cannot precede parameter r in any language
(B) r++ is invalid inside functions
(C) void functions cannot take arguments
(D) Reference parameter syntax '&r' belongs to C++, not standard C
Correct Answer: (D)
[EN] ISO C supports pointers (*r), whereas reference parameters (&r) are a C++ language feature.
[HI] '&r' रेफरेंस पैरामीटर C++ की विशेषता है, मानक C भाषा में इसके लिए पॉइंटर (*r) का उपयोग किया जाता है।
Q8. Which parameter declaration grants zero-copy pointer efficiency while preventing the callee from modifying caller's struct?
(कौन सा पैरामीटर डिक्लेरेशन बड़े struct को बिना कॉपी किए पास करता है और फंक्शन द्वारा उसमें बदलाव भी रोकता है?)
(A) void printRec(struct Record r)
(B) void printRec(struct Record * const r)
(C) void printRec(const struct Record *r)
(D) void printRec(volatile struct Record *r)
Correct Answer: (C)
[EN] 'const struct Record *r' makes the pointed struct read-only via *r while still copying only a single pointer address.
[HI] 'const struct Record *r' डेटा को रीड-ओनली बनाकर सुरक्षित रखता है और केवल पॉइंटर पते की कॉपी बनाता है।
Q9. What typically happens if 'void setZero(int *p) { *p = 0; }' is invoked as 'setZero(NULL);' without a NULL check?
(बिना जांच किए 'void setZero(int *p) { *p = 0; }' को 'setZero(NULL);' से कॉल करने पर सामान्यतः क्या होगा?)
(A) Compile-time error
(B) Undefined Behavior (typically a Segmentation Fault crash)
(C) 0 is safely ignored
(D) p allocates a new int automatically
Correct Answer: (B)
[EN] Dereferencing NULL attempts to access invalid memory address zero, crashing the process on protected operating systems.
[HI] NULL पॉइंटर को डीरेफरेंस (*p) करने से अमान्य मेमोरी एक्सेस होती है जिससे सामान्यतः Segmentation Fault क्रैश आता है।
Q10. Inside 'void promote(struct Emp *e)', how is the 'salary' member of the caller's struct accessed cleanly?
('void promote(struct Emp *e)' के भीतर कॉलर के struct के 'salary' सदस्य को कैसे एक्सेस किया जाता है?)
(A) &e->salary
(B) e->salary
(C) e.salary
(D) *e.salary
Correct Answer: (B)
[EN] For a struct pointer, use the arrow operator e->salary (equivalent to (*e).salary; note *e.salary fails due to precedence).
[HI] स्ट्रक्चर पॉइंटर के सदस्य के लिए तीर ऑपरेटर e->salary (या (*e).salary) उपयोग होता है; *e.salary में डॉट की प्राथमिकता अधिक होने से त्रुटि आएगी।
Q11. On a 64-bit machine, if 'struct Big { double d[500]; };' (4000 bytes) is passed to 'void proc(struct Big *b)', how many argument bytes push onto the stack?
(64-बिट सिस्टम पर 4000 बाइट वाले 'struct Big' को 'void proc(struct Big *b)' से पास करने पर स्टैक पर कितने बाइट कॉपी होंगे?)
(A) 8 bytes (only the pointer address)
(B) 500 bytes
(C) 4000 bytes
(D) 0 bytes
Correct Answer: (A)
[EN] Call by Reference via pointers copies strictly the size of one memory address (8 bytes on 64-bit systems).
[HI] पॉइंटर द्वारा पास करने पर पूरे 4000 बाइट के बजाय केवल एक मेमोरी पते के 8 बाइट ही स्टैक पर जाते हैं।
Q12. What does 'void addSelf(int *x, int *y) { *x += *y; *y += *x; }' produce in 'a' when called as 'int a = 3; addSelf(&a, &a);'?
(यदि 'int a = 3;' के लिए 'addSelf(&a, &a);' कॉल करें जहाँ 'void addSelf(int *x, int *y) { *x += *y; *y += *x; }', तो a क्या होगा?)
(A) 12
(B) 3
(C) 6
(D) 9
Correct Answer: (A)
[EN] Both x and y alias 'a': *x += *y adds 3+3 so a becomes 6; then *y += *x adds 6+6 so a becomes 12!
[HI] x और y दोनों एक ही वेरिएबल 'a' की ओर इशारा कर रहे हैं (Aliasing): पहले स्टेप में a=6 और दूसरे में 6+6 = 12 हो जाता है।
Q13. If a function needs to change where a caller's pointer 'struct Node *head' itself points, what parameter type is required?
(यदि कोई फंक्शन कॉलर के पॉइंटर 'struct Node *head' के स्वयं के पते को बदलना चाहे, तो पैरामीटर किस टाइप का होना चाहिए?)
(A) void *head
(B) struct Node head
(C) struct Node **headRef (pointer to pointer, passed as &head)
(D) struct Node *head
Correct Answer: (C)
[EN] To modify a caller's pointer variable itself via Call by Reference, you must pass the pointer's address (&head) into a double pointer.
[HI] किसी पॉइंटर वेरिएबल का मान (पता) बदलने के लिए उसका पता (&head) एक डबल पॉइंटर (Node **) में लेना पड़ता है।
Q14. In C parameter declarations, how does 'void fill(int arr[], int n)' compare to 'void fill(int *arr, int n)'?
(C फंक्शन पैरामीटर में 'void fill(int arr[], int n)' और 'void fill(int *arr, int n)' में क्या अंतर है?)
(A) arr[] works only for 10 elements
(B) arr[] copies the full array by value whereas *arr passes address
(C) They are completely identical to the C compiler
(D) *arr prevents modifying arr[0]
Correct Answer: (C)
[EN] In parameter lists, array notation [] is purely syntactic sugar that decays directly into a pointer parameter.
[HI] फंक्शन पैरामीटर सूची में int arr[] और int *arr दोनों कंपाइलर के लिए बिल्कुल समान (पॉइंटर पैरामीटर) होते हैं।
Q15. Which statement is NOT true about Call by Reference (using pointers) in C?
(C में पॉइंटर आधारित कॉल बाई रेफरेंस के संबंध में इनमें से कौन सा कथन असत्य है?)
(A) Dereferencing an unvalidated NULL pointer causes Undefined Behavior
(B) Passing structs via pointers avoids copying large data blocks
(C) It enables a single function to produce multiple updated outputs
(D) Pointers allow modifying caller variables only if those variables are declared global
Correct Answer: (D)
[EN] Passing &localVar gives a callee direct access to caller local variables across stack frames without needing globals.
[HI] ग्लोबल वेरिएबल होना जरूरी नहीं है; पॉइंटर के जरिए एक फंक्शन कॉलर के लोकल स्टैक वेरिएबल्स में भी सीधा बदलाव कर सकता है।
Q16. Inside 'void inc(int *p)', why does '*p++' fail to increment the caller's integer while '(*p)++' succeeds?
('void inc(int *p)' में '*p++' कॉलर की संख्या को क्यों नहीं बढ़ाता जबकि '(*p)++' बढ़ा देता है?)
(A) Postfix ++ has higher precedence than unary *, so *p++ advances the local pointer p instead of incrementing *p
(B) Postfix ++ cannot apply to integers
(C) *p++ divides the pointer by 2
(D) (*p)++ changes only the pointer address
Correct Answer: (A)
[EN] Because postfix ++ binds tighter than *, '*p++' parses as '*(p++)' (moving the local copy p); parentheses '(*p)++' target the pointed int.
[HI] पोस्टफिक्स ++ की प्राथमिकता * से अधिक है, इसलिए *p++ स्थानीय पॉइंटर p को आगे बढ़ाता है, जबकि (*p)++ मूल मान को 1 बढ़ाता है।
Q17. Within Parameter Passing: Call by Reference (Using Pointers), which aspect directly maps to: "Pass &x into formal pointer int *p"?
(Parameter Passing: Call by Reference (Using Pointers) में कौन सा Aspect सीधे "Pass &x into formal pointer int *p" से संबंधित है?)
(A) Struct Efficiency
(B) Call & Header Syntax
(C) C Pointer vs C++ Ref
(D) Caller Mutation Effect
Correct Answer: (B)
[EN] In Parameter Passing: Call by Reference (Using Pointers), 'Call & Header Syntax' handles 'Pass &x into formal pointer int *p' (Call by Value passes x directly into int p).
[HI] Parameter Passing: Call by Reference (Using Pointers) में 'Call & Header Syntax' का कार्य/विवरण 'Pass &x into formal pointer int *p' (Call by Value passes x directly into int p) है।
Q18. Which option accurately describes 'Caller Mutation Effect' in Parameter Passing: Call by Reference (Using Pointers)?
(Parameter Passing: Call by Reference (Using Pointers) के संदर्भ में 'Caller Mutation Effect' की सही विशेषता कौन सी है?)
(A) C pointers may be NULL and use * and &
(B) Copies only 4/8 byte address onto stack
(C) Pass &x into formal pointer int *p
(D) Writing *p modifies caller memory permanently
Correct Answer: (D)
[EN] 'Caller Mutation Effect' provides 'Writing *p modifies caller memory permanently' — key point: Enables swaps and multiple output arguments.
[HI] 'Caller Mutation Effect' का मुख्य विवरण 'Writing *p modifies caller memory permanently' है (Enables swaps and multiple output arguments)।
Q19. In Parameter Passing: Call by Reference (Using Pointers), identify the correct parameter for the specification: "Copies only 4/8 byte address onto stack".
(Parameter Passing: Call by Reference (Using Pointers) में "Copies only 4/8 byte address onto stack" विवरण किस विकल्प के लिए सही है?)
(A) Struct Efficiency
(B) Call & Header Syntax
(C) C Pointer vs C++ Ref
(D) Caller Mutation Effect
Correct Answer: (A)
[EN] 'Struct Efficiency' corresponds to 'Copies only 4/8 byte address onto stack' (Use const T* for zero-copy read-only safety).
[HI] 'Struct Efficiency' का संबंध 'Copies only 4/8 byte address onto stack' (Use const T* for zero-copy read-only safety) से है।
Q20. Considering Parameter Passing: Call by Reference (Using Pointers), what key behavior defines 'C Pointer vs C++ Ref'?
(Parameter Passing: Call by Reference (Using Pointers) में 'C Pointer vs C++ Ref' की मुख्य भूमिका या मान क्या है?)
(A) C pointers may be NULL and use * and &
(B) Copies only 4/8 byte address onto stack
(C) Writing *p modifies caller memory permanently
(D) Pass &x into formal pointer int *p
Correct Answer: (A)
[EN] 'C Pointer vs C++ Ref' represents 'C pointers may be NULL and use * and &' (C++ int &r cannot be NULL and needs no *).
[HI] 'C Pointer vs C++ Ref' का अर्थ/मान 'C pointers may be NULL and use * and &' है (C++ int &r cannot be NULL and needs no *)।
Parameter Passing: Call by Reference (Using Pointers) — Exam FAQs
Q1: Why does C use pointers (* and &) for Call by Reference instead of C++ references (&)?
C में कॉल बाई रेफरेंस के लिए C++ रेफरेंस (&) के बजाय पॉइंटर्स (* व &) का उपयोग क्यों होता है?
[EN] C was designed with transparent, explicit memory semantics: requiring & at the call site makes it immediately obvious to readers that the callee may mutate those caller variables, whereas C++ references hide mutation behind value-like call syntax.
[HI] C को पारदर्शी मेमोरी मॉडल के साथ बनाया गया है: कॉल के समय '&' लगाने से कोड पढ़ने वाले को स्पष्ट दिखता है कि फंक्शन उस वेरिएबल का मान बदल सकता है।
Q2: When should you write 'const int *p' as a pointer parameter?
पॉइंटर पैरामीटर में 'const int *p' का उपयोग कब करना चाहिए?
[EN] Whenever you pass a pointer or array solely for efficiency or reading—without intending to mutate the caller's data—so the compiler catches accidental writes at compile time.
[HI] जब केवल गति व मेमोरी बचत के लिए पता या एरे पास किया जाए और उसमें बदलाव न करना हो, ताकि कंपाइलर अनचाहे संशोधन को रोक सके।
Q3: How can a C function modify a caller's pointer itself (e.g., updating linked list head)?
कोई C फंक्शन कॉलर के पॉइंटर वेरिएबल (जैसे लिंक्ड लिस्ट का head) को स्वयं कैसे बदल सकता है?
[EN] Pass the address of the pointer (&head) into a double-pointer parameter (struct Node **headRef) and update *headRef inside the function.
[HI] उस पॉइंटर का स्वयं का पता (&head) एक डबल पॉइंटर पैरामीटर (Node **headRef) में पास करके *headRef के माध्यम से उसे अपडेट करें।
Post a Comment
Post a Comment