C++ में Pointer Dereferencing
In this page:
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 *
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक Pointer के जरिए एक Value पढ़ना
एक बार एक pointer के पास एक valid address हो, इसे कितनी भी बार dereference करना उस address पर current value पढ़ता है, pointer set होने के बाद बदलने पर भी हमेशा latest value reflect करते हुए।
उदाहरण: Reading a Value Through a Pointer
#include <iostream>
int main() {
int value = 10;
int *ptr = &value;
value = 20;
std::cout << *ptr << std::endl; // reflects the latest value
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Dereferencing और Data Types
जिस type से एक pointer declared था वह decide करता है कि dereference होने पर कितने bytes पढ़े जाते हैं और resulting value कैसे interpret होती है, यही वजह है कि एक pointer का declared type उससे match होना चाहिए जिसकी ओर यह actually point करता है।
उदाहरण: Dereferencing and Data Types
#include <iostream>
int main() {
double value = 3.14;
double *ptr = &value;
std::cout << *ptr << std::endl; // reads 8 bytes as a double
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Null Pointer Dereference करना
एक null pointer को dereference करना, जिसे एक valid address assign नहीं हुआ, undefined behavior है और typically program crash करता है, इसलिए dereference करने से पहले एक pointer हमेशा check किया जाना चाहिए या valid होना known होना चाहिए।
उदाहरण: Dereferencing a Null Pointer
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Arrow -> के साथ Dereferencing Combined
एक struct या class के pointer के लिए, (*ptr).member और ptr->member pointer dereference करने और इसके किसी member को access करने के equivalent तरीके हैं, -> ज़्यादा छोटा, ज़्यादा common form है।
उदाहरण: Dereferencing Combined with Arrow ->
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक null या uninitialized pointer dereference करना, जो crash करता है।
*भूल जाना और value की बजाय address print करना।- एक
void*को पहले cast किए बिना dereference करना।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: