C++ में Pointers से Values बदलना
In this page:
*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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक Pointer के जरिए Arithmetic से Modify करना
एक dereferenced pointer एक arithmetic expression की किसी भी side पर appear हो सकता है, pointed-to value को पढ़ने, modify होने, और in place वापस लिखे जाने देते हुए, exactly जैसे सीधे एक variable modify करना।
उदाहरण: Modifying with Arithmetic Through 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;
*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;
}
Login to try C/C++/Java/PHP code in the editor
एक 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक Pointer के जरिए Array Elements Modify करना
क्योंकि एक array का नाम इसके पहले element के एक pointer में decay होता है, pointer arithmetic को dereferencing के साथ combine करके individual array elements को सीधे पढ़ा और लिखा जा सकता है।
उदाहरण: Modifying Array Elements Through a Pointer
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक Struct के Pointer के जरिए Modify करना
-> operator dereferencing को member access के साथ combine करता है, एक struct के members को उस struct के pointer के जरिए सीधे modify होने देते हुए।
उदाहरण: Modifying via a Pointer to a Struct
// 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;
}
Login to try C/C++/Java/PHP code in the editor
*भूल जाना और*p = 10;की बजायp = 10;लिखना, जो pointer को खुद बदलने की कोशिश करता है और एक int pointer के लिए compile नहीं होता।(*p)++का मतलब होने पर*p++लिखना, जो value increment करने की बजाय pointer move करता है।- एक pointer के जरिए लिखना जिसका target variable scope से बाहर जा चुका हो, जो एक dangling pointer और undefined behavior है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: