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

C++ Modifying Values via Pointers

Assigning to a dereferenced pointer, *ptr = value, writes through the pointer to change the original variable's value, which is how functions modify their caller's data via pointer parameters.
Syntax
cpp
*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

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

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

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

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

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

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

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

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

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. #}
Common Mistakes
  1. Forgetting the * and writing p = 10; instead of *p = 10;, which tries to change the pointer itself and does not compile for an int pointer.
  2. Writing *p++ when you mean (*p)++, which moves the pointer instead of incrementing the value.
  3. Writing through a pointer whose target variable has gone out of scope, which is a dangling pointer and 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.