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

C में Pointer to Pointer

एक pointer to a pointer एक note है जो आपको बताता है कि दूसरा note कहाँ store है, और वह note आपको बताता है कि असली चीज़ कहाँ है। आप इसे एक-एक step follow करते हैं।
Syntax
c
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?

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

Declare और Initialize करना

declaration किसी भी type के लिए इस्तेमाल होने वाले double-pointer syntax जैसा ही है: int pointers के pointers के लिए int **pptr, string pointers के arrays के लिए char **names, वगैरह -- asterisks से पहले का type उससे match होना चाहिए जिसकी ओर आखिरकार point किया जा रहा है।

उदाहरण: Declaring and Initializing

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

एक बार Dereference करना

यह आपको chain में एक intermediate address देता है -- अगर pptr, ptr की ओर point करता है, तो *pptr आपको ptr की value देता है, जो खुद एक address है (वह जिसकी ओर ptr point करता है), अभी final data नहीं।

उदाहरण: Dereferencing Once

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("%p", (void*)*pptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

दो बार Dereference करना

यह वह operation है जो आखिरकार indirection chain के नीचे actual stored data तक पहुँचता है, pointer addresses की दोनों layers पार करके असली value तक पहुँचते हुए।

उदाहरण: Dereferencing Twice

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

Functions में Pointers Modify करना

यह pattern उन functions में common है जो memory allocate करते हैं और caller को एक parameter के जरिए नया address वापस देना चाहते हैं, क्योंकि एक single pointer parameter आपको सिर्फ caller के pointer की एक copy modify करने देगा, original नहीं।

उदाहरण: Modifying Pointers in Functions

c
// 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;
}
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. सिर्फ एक बार dereference करना और *pp को value मानना, जब यह अभी भी एक pointer है।
  2. एक pointer to a pointer को एक valid address assign करने से पहले इस्तेमाल करना।
  3. levels का track खो देना, उदाहरण के लिए &p pass करना जहाँ p expected है, जो एक 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।

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.