C में Pointer to Pointer
In this page:
data_type **pointer_to_pointer = &pointer_name;
**pointer_to_pointer;
Pointer to a Pointer क्या है?
यह chain exactly वह mechanism है जो argv main(int argc, char **argv) में इस्तेमाल करता है -- argv pointers के एक array का एक pointer है, जहाँ हर element एक command-line argument string की ओर point करता है।
उदाहरण: What is a Pointer to a Pointer?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
char *args[2] = {"prog", "arg1"};
char **argv = args;
// Print formatted text to the screen
printf("%s", argv[1]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Declare और Initialize करना
declaration किसी भी type के लिए इस्तेमाल होने वाले double-pointer syntax जैसा ही है: int pointers के pointers के लिए int **pptr, string pointers के arrays के लिए char **names, वगैरह -- asterisks से पहले का type उससे match होना चाहिए जिसकी ओर आखिरकार point किया जा रहा है।
उदाहरण: Declaring and Initializing
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
int *ptr = &x;
int **pptr = &ptr;
// Print formatted text to the screen
printf("%d", **pptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक बार Dereference करना
यह आपको chain में एक intermediate address देता है -- अगर pptr, ptr की ओर point करता है, तो *pptr आपको ptr की value देता है, जो खुद एक address है (वह जिसकी ओर ptr point करता है), अभी final data नहीं।
उदाहरण: Dereferencing Once
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
int *ptr = &x;
int **pptr = &ptr;
// Print formatted text to the screen
printf("%p", (void*)*pptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
दो बार Dereference करना
यह वह operation है जो आखिरकार indirection chain के नीचे actual stored data तक पहुँचता है, pointer addresses की दोनों layers पार करके असली value तक पहुँचते हुए।
उदाहरण: Dereferencing Twice
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
int *ptr = &x;
int **pptr = &ptr;
// Print formatted text to the screen
printf("%d", **pptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Functions में Pointers Modify करना
यह pattern उन functions में common है जो memory allocate करते हैं और caller को एक parameter के जरिए नया address वापस देना चाहते हैं, क्योंकि एक single pointer parameter आपको सिर्फ caller के pointer की एक copy modify करने देगा, original नहीं।
उदाहरण: Modifying Pointers in Functions
// Include standard input/output (printf, scanf)
#include <stdio.h>
void allocate(int **out) {
static int value = 10;
*out = &value;
}
// Program execution starts in main()
int main() {
int *ptr = NULL;
allocate(&ptr);
// 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
- सिर्फ एक बार dereference करना और
*ppको value मानना, जब यह अभी भी एक pointer है। - एक pointer to a pointer को एक valid address assign करने से पहले इस्तेमाल करना।
- levels का track खो देना, उदाहरण के लिए
&ppass करना जहाँpexpected है, जो एक type mismatch देता है।
- एक pointer एक memory address store करता है, और pointer arithmetic memory में एक बार में एक element move करता है।
- Pointers arrays और functions से closely connect होते हैं, functions को caller के data पर सीधे काम करने देते हुए।
- Null, void, और double pointers special cases cover करते हैं, जैसे empty pointers, generic pointers, और pointers to pointers।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: