Pointers to Pointers (Double Pointers **ptr) in C Notes in Hindi & English : डबल पॉइंटर्स (**ptr): पॉइंटर का पता स्टोर करना और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Pointers to Pointers (Double Pointers **ptr) in C (डबल पॉइंटर्स (**ptr): पॉइंटर का पता स्टोर करना) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: A double pointer (T **pp) stores the memory address of a single pointer (T *p), enabling two levels of dereferencing to read or mutate the target object and the pointer itself. It is essential in C for updating caller pointers inside functions, building jagged 2D arrays, and handling command-line arguments (char **argv).

अध्याय सार: डबल पॉइंटर (T **pp) किसी सिंगल पॉइंटर (T *p) का मेमोरी एड्रेस स्टोर करता है, जिससे दो स्तर की डीरेफरेंसिंग द्वारा मूल डेटा व पॉइंटर दोनों को बदला जा सकता है। C में फंक्शन के भीतर कॉलर का पॉइंटर बदलने, जैग्ड 2D एरे बनाने और कमांड-लाइन आर्ग्यूमेंट्स (char **argv) के लिए इसका व्यापक उपयोग होता है।

Pointers to Pointers (Double Pointers **ptr) in C: Detailed Notes & Architecture (डबल पॉइंटर्स (**ptr): पॉइंटर का पता स्टोर करना)

1. Multi-Level Indirection & Syntax Mechanics (बहु-स्तरीय इंडायरेक्शन और सिंटैक्स कार्यप्रणाली)

[EN] While a single pointer int *p holds the address of an integer variable x, a pointer-to-pointer int **pp holds the address of the pointer variable p (&p). Dereferencing once (*pp) yields the value stored inside p (which is &x), and dereferencing twice (**pp) reaches the underlying integer x itself. You can chain further levels (int ***ppp), though double pointers are the standard limit in practical systems engineering.

[HI] जहाँ एक सिंगल पॉइंटर int *p किसी इंटीजर वेरिएबल x का पता रखता है, वहीं डबल पॉइंटर int **pp उस पॉइंटर p का पता (&p) स्टोर करता है। एक बार डीरेफरेंस (*pp) करने पर p का मान (&x) प्राप्त होता है, और दो बार डीरेफरेंस (**pp) करने पर वास्तविक मान x मिलता है। इसी प्रकार ट्रिपल पॉइंटर (***ppp) भी बनाए जा सकते हैं, परंतु व्यावहारिक प्रोग्रामिंग में डबल पॉइंटर सर्वाधिक उपयोगी होता है।

2. Pointer Sizes, Arithmetic & Type Compatibility (पॉइंटर साइज, अंकगणित और टाइप संगतता)

[EN] Every pointer variable—whether char *, int *, or double **—occupies the same architecture pointer width (typically 4 bytes on 32-bit ILP32 or 8 bytes on 64-bit LP64 systems). Crucially, pointer arithmetic on T **pp advances by sizeof(T *), NOT sizeof(T): incrementing pp + 1 adds 8 bytes on a 64-bit machine even if T is a 1-byte char. Also beware a common exam trap: a 2D array int mat[3][4] decays to int (*)[4], which is NOT type-compatible with int **.

[HI] किसी भी आर्किटेक्चर पर प्रत्येक पॉइंटर वेरिएबल—चाहे वह char *, int * हो या double **—समान मेमोरी स्थान (32-बिट पर 4 बाइट, 64-बिट पर 8 बाइट) घेरता है। ध्यान दें कि T **pp पर पॉइंटर अंकगणित sizeof(T) के बजाय sizeof(T *) के अनुसार चलता है, अर्थात 64-बिट पर char ** को +1 बढ़ाने पर वह 1 बाइट नहीं बल्कि 8 बाइट आगे बढ़ता है। साथ ही int mat[3][4] का डिके int (*)[4] होता है, जिसे सीधे int ** को असाइन नहीं किया जा सकता।

3. Modifying Caller Pointers Inside C Functions (फंक्शन के भीतर कॉलर का पॉइंटर बदलना)

[EN] Because C passes all arguments strictly by value, passing a single pointer p to void allocMem(int *q) copies only the address stored in p; executing q = malloc(...) inside the callee fails to update p in main(). Passing &p to void allocMem(int **q) lets the function execute *q = malloc(...), directly reassigning the caller's pointer. This pattern underpins linked-list head updates (insertFront(Node **head)) and safe cleanup helpers that reset pointers to NULL.

[HI] C में सभी आर्ग्यूमेंट Call by Value से जाते हैं; अतः void allocMem(int *q) में q = malloc(...) करने पर कॉलर का मूल पॉइंटर p नहीं बदलता। कॉलर के पॉइंटर को बदलने के लिए &p को void allocMem(int **q) में पास किया जाता है ताकि *q = malloc(...) सीधे मूल पॉइंटर को नया पता दे सके। लिंक्ड लिस्ट में हेड नोड बदलने (insertFront(Node **head)) और मेमोरी फ्री करके पॉइंटर को NULL करने में यही तकनीक उपयोग होती है।

4. Dynamic 2D Jagged Arrays & Command-Line argv (डायनामिक 2D जैग्ड एरे व कमांड-लाइन argv)

[EN] Double pointers dynamically build flexible 2D structures: allocating int **grid = malloc(R * sizeof(int *)); creates an array of R row pointers, and looping grid[i] = malloc(cols[i] * sizeof(int)); permits rows of varying lengths (jagged/ragged arrays). Element grid[i][j] evaluates via double indirection *(*(grid + i) + j). Standard C entry point int main(int argc, char **argv) likewise uses a double pointer to access command-line strings, requiring individual row frees before freeing grid itself.

[HI] डबल पॉइंटर द्वारा डायनामिक 2D एरे बनाए जाते हैं: पहले R रो-पॉइंटर्स का एरे malloc(R * sizeof(int *)) से बनता है और फिर प्रत्येक grid[i] के लिए अलग आकार की पंक्तियाँ (Jagged Array) आवंटित की जाती हैं। इसका तत्व grid[i][j] दोहरी डीरेफरेंसिंग *(*(grid + i) + j) से पढ़ा जाता है। कमांड-लाइन स्ट्रिंग्स के लिए main(int argc, char **argv) भी डबल पॉइंटर का उदाहरण है; डायनामिक 2D एरे को फ्री करते समय पहले सभी पंक्तियाँ और अंत में मुख्य पॉइंटर फ्री किया जाता है।

Syntax, Formula & Technical Example (Pointers to Pointers (Double Pointers **ptr) in C)

#include <stdio.h>
void resetToNull(int **q) { *q = NULL; }
int main(void) {
    int x = 42, *p = &x, **pp = &p;
    **pp = 99;
    printf("x=%d, *pp==&x? %d\n", x, (*pp == &x)); // 99, 1
    resetToNull(&p);
    printf("p is NULL? %d\n", p == NULL); // 1
    return 0;
}

Pointers to Pointers (Double Pointers **ptr) in C Quick Reference & Comparison Table

ExpressionValue & Indirection LevelType & Behavior
ppHolds address of pointer variable p (&p)Type int **; size equals pointer width (4 or 8 B)
*ppYields value inside p (address &x)Single dereference; type int *
**ppAccesses target integer value xDouble dereference; type int
pp + 1Skips ahead by sizeof(int *) bytesSteps by pointer size, not sizeof(int)

Pointers to Pointers (Double Pointers **ptr) in C Top 20 Bilingual MCQs with Answers

Q1. What does a double pointer declared as int **pp; directly store?

(int **pp; के रूप में घोषित डबल पॉइंटर सीधे क्या स्टोर करता है?)

(A) The sum of two array addresses

(B) The value of an integer variable directly

(C) The memory address of an int * pointer variable

(D) Two separate integer values side by side

Correct Answer: (C)

[EN] A pointer-to-pointer holds the address of another pointer variable (&p), which in turn points to an int.

[HI] डबल पॉइंटर किसी अन्य पॉइंटर वेरिएबल का पता (&p) रखता है, जो आगे एक int की ओर इशारा करता है।

Q2. Given int x = 10, *p = &x, **pp = &p;, which expression equals &x?

(यदि int x = 10, *p = &x, **pp = &p; हो, तो इनमें से कौन सा एक्सप्रेशन &x के बराबर है?)

(A) &pp

(B) **pp

(C) *pp

(D) pp

Correct Answer: (C)

[EN] *pp dereferences pp once, yielding the contents of p, which is &x.

[HI] pp को एक बार डीरेफरेंस (*pp) करने पर p का मान अर्थात &x प्राप्त होता है।

Q3. On a 64-bit operating system, what is sizeof(char **) compared to sizeof(double **)?

(64-बिट ऑपरेटिंग सिस्टम पर sizeof(char **) की तुलना में sizeof(double **) कितना होता है?)

(A) double ** is 8 times larger than char **

(B) char ** is 1 byte and double ** is 8 bytes

(C) Both are equal (8 bytes each)

(D) char ** is 8 bytes and double ** is 64 bytes

Correct Answer: (C)

[EN] All pointer variables on a flat 64-bit architecture store 64-bit addresses and occupy 8 bytes regardless of base type.

[HI] समान 64-बिट आर्किटेक्चर पर डेटा टाइप कुछ भी हो, सभी पॉइंटर्स 8 बाइट ही लेते हैं।

Q4. Why does void alloc(int *q) { q = malloc(sizeof(int)); } fail to change ptr in main() after calling alloc(ptr)?

(alloc(ptr) कॉल करने पर void alloc(int *q) { q = malloc(sizeof(int)); } मुख्य फंक्शन के ptr को क्यों नहीं बदल पाता?)

(A) Single pointers become constants inside functions

(B) q is a local copy of ptr; modifying q does not alter caller's ptr unless int **q and &ptr are passed

(C) malloc() cannot be called inside helper functions

(D) malloc() allocates memory on the stack instead of heap

Correct Answer: (B)

[EN] In C, pointers are passed by value; modifying the caller's pointer itself requires passing its address (&ptr) to int **q.

[HI] C में पॉइंटर भी बाय-वैल्यू पास होता है; कॉलर का मूल पॉइंटर बदलने के लिए &ptr को int **q में भेजना आवश्यक है।

Q5. Trace the output: int a = 12, *p = &a, **q = &p; **q += 8; printf("%d", a);

(आउटपुट बताएं: int a = 12, *p = &a, **q = &p; **q += 8; printf("%d", a);)

(A) Compile-time error

(B) 8

(C) 20

(D) 12

Correct Answer: (C)

[EN] **q directly aliases a, so **q += 8 adds 8 to a, changing it from 12 to 20.

[HI] **q सीधे वेरिएबल a तक पहुंचता है, इसलिए **q += 8 से a का मान 12 से बढ़कर 20 हो जाता है।

Q6. Why does int mat[3][4]; int **pp = mat; trigger a compilation error or incompatible pointer warning in C?

(int mat[3][4]; int **pp = mat; लिखने पर C में कंपाइल त्रुटि/चेतावनी क्यों आती है?)

(A) int ** only works for float matrices

(B) mat decays to int *** instead of int **

(C) mat decays to int (*)[4] (pointer to 4-int array), which is incompatible with int **

(D) A 2D array cannot be accessed via any pointer in C

Correct Answer: (C)

[EN] 2D arrays are contiguous blocks decaying to array-pointers int (*)[4], NOT arrays of int * pointers.

[HI] 2D एरे सतत मेमोरी में होता है और int (*)[4] में डिके होता है, जबकि int ** पॉइंटर्स के एरे की ओर इशारा करता है।

Q7. Suppose char **pp holds address 4000 on a 64-bit system where pointer size is 8 bytes. What address does (pp + 1) hold?

(मान लीजिए 64-बिट सिस्टम (पॉइंटर साइज 8 बाइट) पर char **pp का मान 4000 है। (pp + 1) का पता क्या होगा?)

(A) 4016

(B) 4004

(C) 4001

(D) 4008

Correct Answer: (D)

[EN] pp points to char * (size 8 bytes), so pp + 1 advances by sizeof(char *) = 8 bytes to 4008.

[HI] pp एक char * की ओर इशारा करता है जिसका साइज 8 बाइट है, इसलिए pp + 1 करने पर पता 4008 होगा।

Q8. For a dynamic 2D matrix int **mat;, which pointer expression is identical to mat[i][j]?

(डायनामिक 2D मैट्रिक्स int **mat; के लिए कौन सा पॉइंटर एक्सप्रेशन mat[i][j] के समान है?)

(A) *(*(mat + i) + j)

(B) **(mat + i * j)

(C) (*mat + i)[j]

(D) *(mat + i + j)

Correct Answer: (A)

[EN] mat + i points to row pointer i; *(mat + i) gets row i's int *, adding j and dereferencing yields mat[i][j].

[HI] *(mat + i) से i-वीं पंक्ति का पॉइंटर मिलता है और उसमें j जोड़कर डीरेफरेंस करने से mat[i][j] प्राप्त होता है।

Q9. In the standard main signature int main(int argc, char *argv[]), what type does argv actually have inside main?

(मानक मुख्य फंक्शन int main(int argc, char *argv[]) में argv वास्तव में किस प्रकार का होता है?)

(A) char *

(B) char (*)[10]

(C) const char

(D) char **

Correct Answer: (D)

[EN] An array of pointers parameter char *argv[] automatically adjusts to pointer-to-pointer char **argv.

[HI] पैरामीटर सूची में पॉइंटर्स का एरे char *argv[] स्वतः char ** में बदल जाता है।

Q10. What is the ONLY safe deallocation sequence for a dynamic 2D array allocated via R calls to malloc(C * sizeof(int)) inside int **m = malloc(R * sizeof(int *));?

(int **m = malloc(R * sizeof(int *)); और R पंक्तियों के लिए अलग से आवंटित डायनामिक 2D एरे को फ्री करने का सही क्रम क्या है?)

(A) Call free(m) first, then loop free(m[i])

(B) Call free(*m) followed by free(**m)

(C) Only call free(m) once; rows auto-free

(D) Free every row m[0]..m[R-1] in a loop first, then call free(m)

Correct Answer: (D)

[EN] Freeing m first loses all row pointers (memory leak and use-after-free if accessed). Rows must be freed before m.

[HI] पहले free(m) करने से पंक्तियों के पते नष्ट हो जाते हैं; इसलिए पहले प्रत्येक m[i] और अंत में free(m) करना आवश्यक है।

Q11. What structural advantage does a dynamic int ** array have over a static int mat[R][C] array?

(स्टैटिक int mat[R][C] की तुलना में डायनामिक int ** एरे की क्या संरचनात्मक विशेषता होती है?)

(A) Each row can have a different column length (jagged/ragged array)

(B) It runs faster due to single-level indexing

(C) It needs zero calls to malloc() or free()

(D) Every element is guaranteed contiguous in RAM

Correct Answer: (A)

[EN] Each row pointer m[i] is allocated independently, allowing variable row lengths (jagged arrays).

[HI] प्रत्येक m[i] अलग से आवंटित होने के कारण हर पंक्ति की लंबाई भिन्न (Jagged Array) रखी जा सकती है।

Q12. Why do singly linked list functions like insertAtHead(Node **head, int val) accept Node ** instead of Node *?

(सिंगली लिंक्ड लिस्ट का insertAtHead(Node **head, int val) फंक्शन Node * के बजाय Node ** क्यों लेता है?)

(A) To store two linked lists inside one argument

(B) Because Node structs cannot be allocated on the heap

(C) Because Node * cannot access struct members

(D) To allow updating the caller's head pointer when a new node becomes the first node

Correct Answer: (D)

[EN] Inserting at the front changes which node head points to; updating caller's head requires passing &head as Node **.

[HI] आगे नया नोड जोड़ने पर head का पता बदलता है; कॉलर का head अपडेट करने के लिए Node ** आवश्यक है।

Q13. Trace the output: char *langs[] = {"C", "CPP", "JAVA"}; char **p = langs; printf("%s", *++p);

(आउटपुट बताएं: char *langs[] = {"C", "CPP", "JAVA"}; char **p = langs; printf("%s", *++p);)

(A) P

(B) JAVA

(C) C

(D) CPP

Correct Answer: (D)

[EN] p starts pointing to langs[0]; ++p advances p to &langs[1], and *p yields the string "CPP".

[HI] p प्रारंभ में langs[0] पर है; ++p से वह langs[1] पर पहुंचता है और *p से "CPP" प्रिंट होता है।

Q14. If int x = 5, *p = &x, **pp = &p;, what is the type of the expression &pp?

(यदि int x = 5, *p = &x, **pp = &p; हो, तो &pp एक्सप्रेशन का डेटा टाइप क्या है?)

(A) int **

(B) int

(C) int ***

(D) int *

Correct Answer: (C)

[EN] Taking the address of a double pointer (int **) adds one more indirection level, producing a triple pointer int ***.

[HI] डबल पॉइंटर (int **) का पता (&pp) लेने पर एक स्तर और जुड़कर प्रकार int *** हो जाता है।

Q15. Which statement is NOT true about double pointers in C?

(C में डबल पॉइंटर्स के बारे में इनमें से कौन सा कथन सत्य नहीं है?)

(A) Command-line argv in main() can be declared as char **argv

(B) Writing int **pp = &(&x); is valid to initialize a double pointer directly from x

(C) Doubly-dereferencing **pp accesses the underlying value x

(D) pp + 1 increases the address by sizeof(int *) bytes

Correct Answer: (B)

[EN] &x produces a temporary rvalue address, and C forbids applying unary & to an rvalue like &(&x).

[HI] &x एक अस्थायी rvalue पता देता है जिस पर दोबारा & ऑपरेटर (&(&x)) नहीं लगाया जा सकता।

Q16. Compared to a single contiguous 1D/2D block, what is a performance drawback of an int ** 2D matrix?

(एकल सतत ब्लॉक की तुलना में int ** आधारित 2D मैट्रिक्स की मुख्य प्रदर्शन कमी क्या है?)

(A) Compiler forbids passing int ** to functions

(B) It cannot store negative integers

(C) Row index i cannot exceed 255

(D) Non-contiguous rows reduce CPU cache locality and add an extra pointer dereference per lookup

Correct Answer: (D)

[EN] Separate heap allocations scatter rows across memory, hurting spatial cache locality and requiring two memory loads.

[HI] अलग-अलग हीप आवंटन के कारण पंक्तियाँ मेमोरी में बिखरी रहती हैं, जिससे कैश लोकैलिटी घटती है और दो बार मेमोरी पढ़नी पड़ती है।

Q17. Within Pointers to Pointers (Double Pointers **ptr) in C, which expression directly maps to: "Holds address of pointer variable p (&p)"?

(Pointers to Pointers (Double Pointers **ptr) in C में कौन सा Expression सीधे "Holds address of pointer variable p (&p)" से संबंधित है?)

(A) pp + 1

(B) pp

(C) **pp

(D) *pp

Correct Answer: (B)

[EN] In Pointers to Pointers (Double Pointers **ptr) in C, 'pp' handles 'Holds address of pointer variable p (&p)' (Type int **; size equals pointer width (4 or 8 B)).

[HI] Pointers to Pointers (Double Pointers **ptr) in C में 'pp' का कार्य/विवरण 'Holds address of pointer variable p (&p)' (Type int **; size equals pointer width (4 or 8 B)) है।

Q18. Which option accurately describes '*pp' in Pointers to Pointers (Double Pointers **ptr) in C?

(Pointers to Pointers (Double Pointers **ptr) in C के संदर्भ में '*pp' की सही विशेषता कौन सी है?)

(A) Skips ahead by sizeof(int *) bytes

(B) Holds address of pointer variable p (&p)

(C) Yields value inside p (address &x)

(D) Accesses target integer value x

Correct Answer: (C)

[EN] '*pp' provides 'Yields value inside p (address &x)' — key point: Single dereference; type int *.

[HI] '*pp' का मुख्य विवरण 'Yields value inside p (address &x)' है (Single dereference; type int *)।

Q19. In Pointers to Pointers (Double Pointers **ptr) in C, identify the correct parameter for the specification: "Accesses target integer value x".

(Pointers to Pointers (Double Pointers **ptr) in C में "Accesses target integer value x" विवरण किस विकल्प के लिए सही है?)

(A) pp + 1

(B) **pp

(C) pp

(D) *pp

Correct Answer: (B)

[EN] '**pp' corresponds to 'Accesses target integer value x' (Double dereference; type int).

[HI] '**pp' का संबंध 'Accesses target integer value x' (Double dereference; type int) से है।

Q20. Considering Pointers to Pointers (Double Pointers **ptr) in C, what key behavior defines 'pp + 1'?

(Pointers to Pointers (Double Pointers **ptr) in C में 'pp + 1' की मुख्य भूमिका या मान क्या है?)

(A) Accesses target integer value x

(B) Skips ahead by sizeof(int *) bytes

(C) Holds address of pointer variable p (&p)

(D) Yields value inside p (address &x)

Correct Answer: (B)

[EN] 'pp + 1' represents 'Skips ahead by sizeof(int *) bytes' (Steps by pointer size, not sizeof(int)).

[HI] 'pp + 1' का अर्थ/मान 'Skips ahead by sizeof(int *) bytes' है (Steps by pointer size, not sizeof(int))।

Pointers to Pointers (Double Pointers **ptr) in C — Exam FAQs

Q1: When should I actually use a double pointer (**ptr) in C?

C प्रोग्रामिंग में डबल पॉइंटर (**ptr) का उपयोग कब करना चाहिए?

[EN] Use a double pointer whenever a function must modify a pointer variable belonging to its caller (such as allocating a buffer or updating a linked-list head), or when managing an array of strings/pointers like char **argv or a jagged 2D array.

[HI] जब किसी फंक्शन को कॉलर के पॉइंटर वेरिएबल में नया पता डालना हो (जैसे डायनामिक मेमोरी देना या लिंक्ड लिस्ट का head बदलना), या स्ट्रिंग्स का एरे (char **argv) व जैग्ड 2D एरे संभालना हो, तब डबल पॉइंटर उपयोग करें।

Q2: Can I pass a fixed 2D array int arr[3][4] to a function expecting int **p?

क्या फिक्स्ड 2D एरे int arr[3][4] को int **p पैरामीटर वाले फंक्शन में पास कर सकते हैं?

[EN] No. Fixed 2D arrays reside in one contiguous block and decay to int (*)[4], not int **. Attempting to read p[0][0] through int ** interprets integer data as a memory address and crashes.

[HI] नहीं। फिक्स्ड 2D एरे एक सतत ब्लॉक होता है जो int (*)[4] में बदलता है, न कि int ** में। यदि int ** मानकर एक्सेस किया गया, तो एरे के मानों को ही पता मान लेने से प्रोग्राम क्रैश हो जाएगा।

Q3: How is triple pointer (***ppp) related to **pp?

ट्रिपल पॉइंटर (***ppp) का डबल पॉइंटर (**pp) से क्या संबंध है?

[EN] A triple pointer int ***ppp stores the address of a double pointer (&pp), commonly used when a function must allocate or modify an array of pointers (int **) belonging to the caller.

[HI] ट्रिपल पॉइंटर int ***ppp किसी डबल पॉइंटर (&pp) का पता स्टोर करता है, जिसका उपयोग तब होता है जब किसी फंक्शन को कॉलर के int ** पॉइंटर को आवंटित या परिवर्तित करना हो।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)