C के Double Pointers
In this page:
data_type variable_name = value;
data_type *pointer_name = &variable_name;
data_type **double_pointer = &pointer_name;
**double_pointer; // the original value
Double Pointer क्या है?
आप इस indirection को आगे chain कर सकते हैं -- एक triple pointer एक double pointer का address store करता है -- लेकिन practice में, C code शायद ही कभी दो levels से आगे जाता है क्योंकि added complexity benefit से ज़्यादा है।
उदाहरण: What is a Double Pointer?
// 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
Double Pointers Declare करना
int **pptr, pptr को एक int pointer के लिए एक pointer declare करता है; type को हर level पर match करना चाहिए, इसलिए आप एक plain int pointer का address तीन asterisks से declared एक variable को assign नहीं कर सकते।
उदाहरण: Declaring Double Pointers
// 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
Initialize और Reference करना
एक typical setup ऐसा दिखता है: int x = 5; int *ptr = &x; int **pptr = &ptr -- pptr अब वह address रखता है जहाँ ptr खुद store है, x से एक level ऊपर indirection।
उदाहरण: Initializing and Referencing
// 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", *ptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Double Dereferencing (**)
ऊपर के setup को देखते हुए, *pptr ptr देता है (x का address), और **pptr खुद x की value देता है (5) -- हर asterisk indirection की exactly एक layer छीलता है।
उदाहरण: Double Dereferencing ()
// 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
Double Pointers के Practical Uses
यह essential है जब एक function को यह modify करना हो कि एक pointer किसकी ओर point करता है (सिर्फ उस address की value नहीं) -- उदाहरण के लिए, एक function जो memory allocate करता है और caller के pointer variable को खुद नए block की ओर point करने के लिए update करवाना चाहता है।
उदाहरण: Practical Uses of Double Pointers
// Include standard input/output (printf, scanf)
#include <stdio.h>
void allocate(int **out) {
static int value = 99;
*out = &value;
}
// Program execution starts in main()
int main() {
int *result;
allocate(&result);
// Print formatted text to the screen
printf("%d", *result);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
*ppसे सिर्फ एक बार dereference करना और final value की उम्मीद करना, जब original variable तक पहुँचने के लिए**ppचाहिए।- एक double pointer इस्तेमाल करना जो initialize नहीं हुआ, ताकि इसे dereference करना crash करे।
*pp(inner pointer) को**pp(जिस value की ओर यह point करता है) से confuse करना।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: