← Back to C++ Course | Chapter 7: Pointers & References | Lesson 2 of 11

C++ में Pointer Dereferencing

* operator से एक pointer dereference करना इसके stored address को follow करके वहाँ stored actual value access करता है, pointed-to data को पढ़ने या modify होने देते हुए।
Syntax
cpp
data_type *pointer_name = &variable_name;

*pointer_name;  // the value the pointer points to

Dereference Operator *

* operator, एक pointer पर apply होने पर, इसके store किए memory address को follow करता है और वहाँ मिली value तक access देता है -- इसे pointer dereference करना कहते हैं।

उदाहरण: The Dereference Operator *

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare value and set it to 10
	int value = 10;
	int *ptr = &value;
	// Print to the console with cout
	std::cout << *ptr << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक Pointer के जरिए एक Value पढ़ना

एक बार एक pointer के पास एक valid address हो, इसे कितनी भी बार dereference करना उस address पर current value पढ़ता है, pointer set होने के बाद बदलने पर भी हमेशा latest value reflect करते हुए।

उदाहरण: Reading a Value Through a Pointer

cpp
#include <iostream>

int main() {
	int value = 10;
	int *ptr = &value;
	value = 20;
	std::cout << *ptr << std::endl; // reflects the latest value
	return 0;
}

Dereferencing और Data Types

जिस type से एक pointer declared था वह decide करता है कि dereference होने पर कितने bytes पढ़े जाते हैं और resulting value कैसे interpret होती है, यही वजह है कि एक pointer का declared type उससे match होना चाहिए जिसकी ओर यह actually point करता है।

उदाहरण: Dereferencing and Data Types

cpp
#include <iostream>

int main() {
	double value = 3.14;
	double *ptr = &value;
	std::cout << *ptr << std::endl; // reads 8 bytes as a double
	return 0;
}

एक Null Pointer Dereference करना

एक null pointer को dereference करना, जिसे एक valid address assign नहीं हुआ, undefined behavior है और typically program crash करता है, इसलिए dereference करने से पहले एक pointer हमेशा check किया जाना चाहिए या valid होना known होना चाहिए।

उदाहरण: Dereferencing a Null Pointer

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int *ptr = nullptr;
	// Check whether ptr != nullptr
	if (ptr != nullptr) {
		// Print to the console with cout
		std::cout << *ptr << std::endl;
	} else {
		// Print to the console with cout
		std::cout << "Cannot dereference a null pointer" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Arrow -> के साथ Dereferencing Combined

एक struct या class के pointer के लिए, (*ptr).member और ptr->member pointer dereference करने और इसके किसी member को access करने के equivalent तरीके हैं, -> ज़्यादा छोटा, ज़्यादा common form है।

उदाहरण: Dereferencing Combined with Arrow ->

cpp
// Include std::cout and std::cin
#include <iostream>

// Define a structure type named Point
struct Point { int x; int y; };

// Program execution starts in main()
int main() {
	Point p = {3, 4};
	Point *ptr = &p;
	// Print to the console with cout
	std::cout << (*ptr).x << " " << ptr->x << std::endl;
	// 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. एक null या uninitialized pointer dereference करना, जो crash करता है।
  2. * भूल जाना और value की बजाय address print करना।
  3. एक void* को पहले cast किए बिना dereference करना।

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.