← Back to C Course | Chapter 7: Pointers | Lesson 6 of 9

C के Double Pointers

एक double pointer एक pointer है जो दूसरे pointer की ओर point करता है, एक note जो आपको बताता है कि दूसरा note कहाँ मिलेगा। यह किसी function के अंदर एक pointer बदलने के लिए useful है।
Syntax
c
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?

c
// 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;
}

Double Pointers Declare करना

int **pptr, pptr को एक int pointer के लिए एक pointer declare करता है; type को हर level पर match करना चाहिए, इसलिए आप एक plain int pointer का address तीन asterisks से declared एक variable को assign नहीं कर सकते।

उदाहरण: Declaring Double Pointers

c
// 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;
}

Initialize और Reference करना

एक typical setup ऐसा दिखता है: int x = 5; int *ptr = &x; int **pptr = &ptr -- pptr अब वह address रखता है जहाँ ptr खुद store है, x से एक level ऊपर indirection।

उदाहरण: Initializing and Referencing

c
// 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;
}

Double Dereferencing (**)

ऊपर के setup को देखते हुए, *pptr ptr देता है (x का address), और **pptr खुद x की value देता है (5) -- हर asterisk indirection की exactly एक layer छीलता है।

उदाहरण: Double Dereferencing ()

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. *pp से सिर्फ एक बार dereference करना और final value की उम्मीद करना, जब original variable तक पहुँचने के लिए **pp चाहिए।
  2. एक double pointer इस्तेमाल करना जो initialize नहीं हुआ, ताकि इसे dereference करना crash करे।
  3. *pp (inner pointer) को **pp (जिस value की ओर यह point करता है) से confuse करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.