C++ Modifying Values via Pointers
In this page:
*pointer_name = new_value;
*pointer_name = *pointer_name + amount;
Writing Through a Pointer
Assigning a new value to a dereferenced pointer, written *ptr = value, writes that value into the memory location the pointer refers to, changing the original variable.
Example: 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
Modifying with Arithmetic Through a Pointer
A dereferenced pointer can appear on either side of an arithmetic expression, letting the pointed-to value be read, modified, and written back in place, just like modifying a variable directly.
Example: 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
Modifying a Variable Through a Function
A function that receives a pointer parameter can modify the caller's original variable by dereferencing and assigning to it, which is the classic C++ technique for pass-by-pointer.
Example: 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
Modifying Array Elements Through a Pointer
Since an array's name decays to a pointer to its first element, pointer arithmetic combined with dereferencing can be used to read and write individual array elements directly.
Example: 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
Modifying via a Pointer to a Struct
The -> operator combines dereferencing with member access, letting a struct's members be modified directly through a pointer to that struct.
Example: 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
- Forgetting the
*and writingp = 10;instead of*p = 10;, which tries to change the pointer itself and does not compile for an int pointer. - Writing
*p++when you mean(*p)++, which moves the pointer instead of incrementing the value. - Writing through a pointer whose target variable has gone out of scope, which is a dangling pointer and undefined behavior.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: