C++ Pointers का परिचय
In this page:
data_type *pointer_name = &variable_name; // store the address of a variable
Pointer क्या है?
एक pointer एक variable है जिसकी value ordinary data की बजाय एक memory address है, आपको किसी दूसरे variable के सिर्फ value की बजाय यह memory में exactly कहाँ रहता है इसे refer और manipulate करने देते हुए।
यह indirection ही है जो dynamic memory allocation और बिना copy किए efficiently बड़ा data pass करने जैसी techniques enable करता है।
उदाहरण: What is a Pointer?
// 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 << "Address stored: " << 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
Pointers Declare और Initialize करना
एक pointer declare करना type और variable नाम के बीच एक asterisk रखता है, जैसे int *ptr;, और address-of operator & एक variable का actual memory address retrieve करता है, इसलिए ptr = &age; ptr को सीधे उस जगह point कराता है जहाँ age memory में रहता है।
उदाहरण: Declaring and Initializing Pointers
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare value and set it to 42
int value = 42;
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
Pointers Dereference करना
Dereference operator, जिसे भी * लिखा जाता है लेकिन यहाँ अलग तरह से इस्तेमाल होता है — *ptr — pointer के hold किए address पर actually stored value access या modify करता है, address खुद नहीं।
तो *ptr = 30; pointer के जरिए original variable की value बदलता है, pointer को खुद नहीं।
उदाहरण: Dereferencing Pointers
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare value and set it to 5
int value = 5;
int *ptr = &value;
*ptr = 20;
// Print to the console with cout
std::cout << value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Memory Addresses Print करना
एक pointer variable को बिना dereference किए सीधे print करना यह address output करता है जो यह holds करता है — आमतौर पर 0x7ffee3a1c05c जैसे एक hexadecimal number की तरह दिखता है — जो debugging के लिए useful है लेकिन आपको उस address पर actually stored value के बारे में कुछ नहीं बताता जब तक आप इसे भी dereference न करें।
उदाहरण: Printing Memory Addresses
#include <iostream>
int main() {
int value = 5;
int *ptr = &value;
std::cout << ptr << std::endl; // prints the address, not the value
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Null Pointers
एक pointer जो currently किसी valid variable की ओर point नहीं कर रहा उसे uninitialized छोड़ने की बजाय explicitly nullptr set किया जाना चाहिए, क्योंकि एक uninitialized wild pointer एक garbage address रखता है जो memory में कहीं भी point कर सकता है।
एक nullptr को dereference करना अभी भी program crash करता है, लेकिन यह predictably होता है और एक wild pointer से unrelated memory corrupt होने से कहीं ज़्यादा debug करना आसान है।
उदाहरण: Null Pointers
// 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 << "Pointer is not pointing anywhere yet" << 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 को कहीं point करने से पहले इस्तेमाल करना, जैसे
int* p; *p = 5;, जो एक random address पर लिखता है और crash कर सकता है। int* a, b;लिखना और मान लेना कि दोनों pointers हैं, जब सिर्फaएक pointer है औरbएक plainintहै।&x(x का address) को*p(जिस value की ओर p point करता है) से confuse करना और एक value का इरादा होने पर एक address print कर देना।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: