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

C++ में Pointers से Values बदलना

एक dereferenced pointer को assign करना, *ptr = value, original variable की value बदलने के लिए pointer के जरिए लिखता है, जो यह है कि functions pointer parameters के जरिए अपने caller का data कैसे modify करते हैं।
Syntax
cpp
*pointer_name = new_value;

*pointer_name = *pointer_name + amount;

एक Pointer के जरिए लिखना

एक dereferenced pointer को एक नई value assign करना, *ptr = value लिखा गया, उस value को उस memory location में लिखता है जिसे pointer refer करता है, original variable बदलते हुए।

उदाहरण: Writing Through a Pointer

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 = 100;
	// Print to the console with cout
	std::cout << value << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक Pointer के जरिए Arithmetic से Modify करना

एक dereferenced pointer एक arithmetic expression की किसी भी side पर appear हो सकता है, pointed-to value को पढ़ने, modify होने, और in place वापस लिखे जाने देते हुए, exactly जैसे सीधे एक variable modify करना।

उदाहरण: Modifying with Arithmetic Through 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;
	*ptr = *ptr + 5;
	// Print to the console with cout
	std::cout << value << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक Function के जरिए एक Variable Modify करना

एक pointer parameter receive करने वाला एक function इसे dereference करके और इसे assign करके caller का original variable modify कर सकता है, जो pass-by-pointer की classic C++ technique है।

उदाहरण: Modifying a Variable Through a Function

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

void setToZero(int *ptr) {
	*ptr = 0;
}

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

एक Pointer के जरिए Array Elements Modify करना

क्योंकि एक array का नाम इसके पहले element के एक pointer में decay होता है, pointer arithmetic को dereferencing के साथ combine करके individual array elements को सीधे पढ़ा और लिखा जा सकता है।

उदाहरण: Modifying Array Elements Through a Pointer

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

// Program execution starts in main()
int main() {
	int arr[] = {1, 2, 3};
	int *ptr = arr;
	*(ptr + 1) = 99;
	// Print to the console with cout
	std::cout << arr[1] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक Struct के Pointer के जरिए Modify करना

-> operator dereferencing को member access के साथ combine करता है, एक struct के members को उस struct के pointer के जरिए सीधे modify होने देते हुए।

उदाहरण: Modifying via a Pointer to a Struct

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 = {1, 2};
	Point *ptr = &p;
	ptr->x = 100;
	// Print to the console with cout
	std::cout << p.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. * भूल जाना और *p = 10; की बजाय p = 10; लिखना, जो pointer को खुद बदलने की कोशिश करता है और एक int pointer के लिए compile नहीं होता।
  2. (*p)++ का मतलब होने पर *p++ लिखना, जो value increment करने की बजाय pointer move करता है।
  3. एक pointer के जरिए लिखना जिसका target variable scope से बाहर जा चुका हो, जो एक dangling pointer और undefined behavior है।

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.