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

C++ Pointers का परिचय

एक pointer एक variable है जो दूसरे variable का address रखता है, यह बताने वाले एक note की तरह कि कोई चीज़ किस locker में है। आप उस चीज़ तक पहुंचने के लिए note follow करते हैं।
Syntax
cpp
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?

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 << "Address stored: " << ptr << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

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

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

Pointers Dereference करना

Dereference operator, जिसे भी * लिखा जाता है लेकिन यहाँ अलग तरह से इस्तेमाल होता है — *ptr — pointer के hold किए address पर actually stored value access या modify करता है, address खुद नहीं।

तो *ptr = 30; pointer के जरिए original variable की value बदलता है, pointer को खुद नहीं।

उदाहरण: Dereferencing Pointers

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

Memory Addresses Print करना

एक pointer variable को बिना dereference किए सीधे print करना यह address output करता है जो यह holds करता है — आमतौर पर 0x7ffee3a1c05c जैसे एक hexadecimal number की तरह दिखता है — जो debugging के लिए useful है लेकिन आपको उस address पर actually stored value के बारे में कुछ नहीं बताता जब तक आप इसे भी dereference न करें।

उदाहरण: Printing Memory Addresses

cpp
#include <iostream>

int main() {
	int value = 5;
	int *ptr = &value;
	std::cout << ptr << std::endl; // prints the address, not the value
	return 0;
}

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

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 << "Pointer is not pointing anywhere yet" << 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. एक pointer को कहीं point करने से पहले इस्तेमाल करना, जैसे int* p; *p = 5;, जो एक random address पर लिखता है और crash कर सकता है।
  2. int* a, b; लिखना और मान लेना कि दोनों pointers हैं, जब सिर्फ a एक pointer है और b एक plain int है।
  3. &x (x का address) को *p (जिस value की ओर p point करता है) से confuse करना और एक value का इरादा होने पर एक address print कर देना।

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.