Back to C++
2026-03-236 min read

Example 2: Changing Value Pointed by Pointers (C++)

Learn Example 2: Changing Value Pointed by Pointers (C++) step by step with clear examples and exercises.

Why This Matters

Understanding how to change values pointed by pointers is essential for mastering C++ programming. Pointers allow you to manipulate memory directly and are crucial for solving complex problems, optimizing code, and preparing for interviews or exams. In this lesson, we will explore the core concepts of changing values pointed by pointers, provide a worked example, discuss common mistakes, offer practice questions, and answer frequently asked questions.

Why This Matters

Manipulating memory directly using pointers is a fundamental skill in C++ programming. Pointers allow for more efficient code, dynamic memory allocation, passing arrays to functions by reference, and the implementation of complex data structures like linked lists and trees. Mastering pointer usage will help you tackle challenging problems, optimize your code, and prepare for interviews and exams.

Prerequisites

Before diving into changing values pointed by pointers, it is essential to have a solid understanding of the following topics:

  • C++ basics (variables, data types, operators)
  • Pointers (declaration, initialization, dereferencing)
  • Arrays (declaration, accessing elements, and passing to functions)

It's also recommended to have some experience with writing and debugging simple C++ programs.

Core Concept

A pointer is a variable that stores the memory address of another variable. To change the value pointed by a pointer, you need to use the operator= or the dereference operator *.

Here's an example:

int num = 5;
int *ptr = # // ptr now points to the memory address of num
*ptr = 10; // assigns the value 10 to the memory location pointed by ptr, changing the value of num

In this example, we first declare an integer num and initialize it with the value 5. We then create a pointer ptr that points to the memory address of num. Finally, we change the value of num by dereferencing the pointer ptr and assigning the new value 10.

Pointers and Arrays

Pointers can also be used with arrays to manipulate multiple variables at once. When you declare a pointer to an array, it points to the first element of the array:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // ptr now points to the memory address of the first element in arr
*(ptr + 1) = 10; // assigns the value 10 to the second element in arr (index 1)

In this example, we create an array arr with five elements and initialize it with values from 1 to 5. We then create a pointer ptr that points to the first element of the array. To change the value of the second element (index 1), we add 1 to the pointer and dereference it, effectively accessing the memory location of the second element in the array.

Pointer Arithmetic

When performing arithmetic with pointers, remember that each element in an array has a size equal to its data type's size (e.g., 4 bytes for an int on most systems). This means that if you want to access the third element of an array, you should increment the pointer by three times its data type's size:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // ptr now points to the first element in arr
*(ptr + 3 * sizeof(int)) = 10; // assigns the value 10 to the third element (index 2)

Worked Example

Let's create a simple program that declares an array, changes some values using pointers, and prints the resulting array:

#include <iostream>
using namespace std;

int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // create a pointer to the first element in the array

cout << "Original Array: ";
for (int i = 0; i < 5; ++i) {
cout << arr[i] << " ";
}
cout << endl;

// change some values using pointers
*(ptr + 1) = 10;
*(ptr + 3) = 20;

cout << "Changed Array: ";
for (int i = 0; i < 5; ++i) {
cout << arr[i] << " ";
}
cout << endl;

return 0;
}

When you run this program, it will output the following:

Original Array: 1 2 3 4 5
Changed Array: 1 10 3 20 5

Common Mistakes

  • Forgetting to dereference a pointer when changing its value: If you forget to use the dereference operator *, you will be trying to change the memory address itself, which is not what you want.
int num = 5;
int *ptr = &num; // ptr now points to the memory address of num
ptr = 10; // this changes the value of ptr, not the value it points to
  • Incorrect pointer arithmetic: Be careful when performing arithmetic with pointers. Remember that each element in an array has a size equal to its data type's size (e.g., 4 bytes for an int on most systems).
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // ptr now points to the first element in arr
*(ptr + 2) = 10; // this changes the value of the third element (index 2), not the fourth (index 3)
  • Not initializing pointers: If you declare a pointer without initializing it, its value is undefined. This can lead to segmentation faults or other unexpected behavior.
int num = 5;
int *ptr; // ptr has an undefined value
ptr = &num; // now ptr points to the memory address of num
  • Using uninitialized pointers: Using a pointer that hasn't been initialized or assigned a valid memory address can lead to segmentation faults or other unexpected behavior.

Practice Questions

  1. Write a program that declares an array of five integers, changes some values using pointers, and prints the resulting array. The changed values should be 20 for the first element, 30 for the third element, and the original value for the rest.
#include <iostream>
using namespace std;

int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // create a pointer to the first element in the array

cout << "Original Array: ";
for (int i = 0; i < 5; ++i) {
cout << arr[i] << " ";
}
cout << endl;

// change some values using pointers
*(ptr + 0) = 20; // change the first element
*(ptr + 3) = 30; // change the third element

cout << "Changed Array: ";
for (int i = 0; i < 5; ++i) {
cout << arr[i] << " ";
}
cout << endl;

return 0;
}
  1. Write a program that declares a string, changes some characters using pointers, and prints the resulting string. The changed characters should be 'A' for the first character and '!' for the third character.
#include <iostream>
using namespace std;

int main() {
char str[] = "Hello, World!";
char *ptr = &str[0]; // create a pointer to the first character in the string

cout << "Original String: " << str << endl;

*(ptr + 0) = 'A'; // change the first character
*(ptr + 6) = '!'; // change the seventh character (index 6, counting from zero)

cout << "Changed String: " << str << endl;

return 0;
}

FAQ

Q: Why do we use pointers in C++?

A: Pointers are powerful tools that allow you to manipulate memory directly, which can lead to more efficient code. They enable dynamic memory allocation, passing arrays to functions by reference, and the implementation of complex data structures like linked lists and trees.

Q: How do I check if a pointer is pointing to a valid memory address?

A: You can use the nullptr keyword or the 0 constant to initialize pointers, indicating that they are not pointing to any valid memory address. To check if a pointer points to a valid memory address, you can use the == nullptr or != 0 comparison operators.

Q: What happens when I try to change the value of a pointer itself instead of the value it points to?

A: If you forget to dereference a pointer when changing its value, you will be changing the memory address itself, which can lead to segmentation faults or other unexpected behavior.

Q: How do I calculate the memory address of a variable in C++?

A: You can get the memory address of a variable using the & operator. For example, if you have an integer num, you can get its memory address with &num.

Q: Can I use pointers to swap two variables without using a temporary variable?

A: Yes, you can use pointers to swap two variables without using a temporary variable. Here's an example:

int num1 = 5;
int num2 = 10;
int *ptr1 = &num1;
int *ptr2 = &num2;
int temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;

In this example, we first create pointers ptr1 and ptr2 that point to the memory addresses of num1 and num2, respectively. We then store the value of num1 in a temporary variable temp. After that, we swap the values of num1 and num2 by exchanging the values pointed by ptr1 and ptr2.

Example 2: Changing Value Pointed by Pointers (C++) | C++ | XQA Learn