Pointer arithmetic
C++ pointer arithmetic allows for performing arithmetic operations on pointer type variables, treating the memory addresses they point to as numeric values. This enables common arithmetic operations such as addition and subtraction to be applied to pointers.
Lets Go!

Pointer arithmetic
Lesson 16
Understand how to perform arithmetic operations on pointers, including incrementing, decrementing, and calculating offsets.
Get Started 🍁Introduction to Pointers Arithmetic in C++
Welcome to the course "Introduction to Pointers Arithmetic in C++"! In this course, we will delve into the fascinating world of pointer arithmetic in the C++ programming language.
Pointers in C++ hold memory addresses, which are essentially numeric values. This opens up the possibility of performing arithmetic operations on pointers, just like you would on other numerical values. You can use operators like plus plus, minus minus, addition, and subtraction on pointer type variables.
Have you ever wondered how arithmetic operations can be applied to memory addresses in C++? Join us as we explore the intricacies of pointer arithmetic, understanding how pointers behave when subjected to mathematical operations.
Throughout this course, we will explore sample code snippets to enhance your understanding. By the end, you'll be able to manipulate pointers effectively, incrementing and decrementing them by the appropriate size based on the data type.
Get ready to unlock the power of pointer arithmetic in C++ and take your programming skills to the next level! Are you ready to dive in and discover the magic of pointers in action? Let's get started!
Main Concepts of Pointers Arithmetic in C++
-
Pointers in C++ allow for arithmetic operations to be performed on variables that hold addresses.
Explanation: Pointers in C++ are variables that store memory addresses. Since these addresses are numeric values, arithmetic operations such as addition, subtraction, incrementation, and decrementation can be applied to pointers just like regular numeric values.
-
Four arithmetic operations that can be used on pointers are:
- Increment (++): Increases the pointer's address by the size of the data type it points to.
- Decrement (--): Decreases the pointer's address by the size of the data type it points to.
- Addition (+): Adds a specified value to the pointer's address.
- Subtraction (-): Subtracts a specified value from the pointer's address.
-
Pointers hold memory locations (addresses) that are represented by numeric values and are essential for applying arithmetic operators on pointer type variables.
Explanation: Memory locations where variables are stored are represented by numeric values. Therefore, arithmetic operators can be utilized to manipulate pointer type variables, ensuring efficient memory management and access.
-
Example code demonstration:
- Given a constant integer max value of 3 and an array of integers initialized with values, a pointer PTR is initialized with the starting address of the array.
- A loop iterates through the array elements, printing the subscript value, address pointed by PTR, and the content at that address (value) using the arithmetic operation
PTR++
to increment the pointer.
-
Size consideration when performing arithmetic operations on pointers:
- Pointer arithmetic in C++ increments or decrements the pointer address by the size of the data type it points to.
Explanation: In C++, the size of a data type, such as an integer, determines the increment or decrement value of a pointer when using arithmetic operators. For instance, increasing a pointer by 1 typically results in an increment of the pointer by the size of the data type, such as 4 bytes for an integer.
Practical Applications of Pointer Arithmetic in C++
Step-by-Step Guide
-
Initialize an Array and a Pointer:
- Define a variable
max
with the value 3 asConst int
. - Create an array variable of type integer with a size of 3, and initialize it with values.
- Declare a pointer variable
PTR
and point it to the starting address of the array.
int max = 3; int arr[3] = {10, 100, 200}; int* PTR = arr; // Assign the starting address of the array to PTR
- Define a variable
-
Iterate through the Array using Pointer Arithmetic:
- Create a for loop with
i
ranging from 0 to 2 (less thanmax
). - Print the index
i
and the content at that index from the array. - Print the address stored in the pointer
PTR
and the content it points to. - Increment the pointer
PTR
using the++
operator.
for (int i = 0; i < max; i++) { cout << "Index: " << i << endl; cout << "Value at Index: " << arr[i] << endl; cout << "Address Stored in PTR: " << PTR << endl; cout << "Value at PTR: " << *PTR << endl; // Dereferencing the pointer PTR++; // Incrementing the pointer by the size of int (4 bytes) }
- Create a for loop with
-
Understanding Pointer Arithmetic:
- Notice that incrementing the pointer
PTR
by 1 actually increases its address by 4 bytes (size of an integer), not by 1. - Experiment with increasing the pointer by different values to observe the corresponding change in the address.
- Notice that incrementing the pointer
Try It Out
-
Copy the code provided above and paste it into your C++ compiler.
-
Run the code to see how pointer arithmetic can be utilized to iterate through an array efficiently.
-
Modify the code by changing the increment value of the pointer to see the impact on the address calculation.
-
Experiment with different array sizes and values to further enhance your understanding of pointer arithmetic in C++.
Get Hands-On and Explore Pointer Arithmetic in C++!
Test your Knowledge
Can pointer arithmetic be used on pointers to void?
Can pointer arithmetic be used on pointers to void?
Advanced Insights into Pointers and Arithmetic Operations
In C++, arithmetic operations can be performed on pointer type variables, allowing us to manipulate memory addresses just like numerical values. You can use operators like ++
, --
, +
, and -
on pointers to navigate memory locations efficiently.
When working with pointers, remember that memory locations are represented by numeric values. Consequently, when you increment or decrement a pointer using ++
or --
, the pointer value changes by the size of the data type it points to. For example, incrementing an integer pointer by 1 will actually increase it by 4 bytes (the size of an integer in memory).
Tips and Recommendations:
- When incrementing or decrementing pointers, consider the size of the data type to avoid unexpected behavior.
- Experiment with different pointer arithmetic operations to gain a deeper understanding of memory manipulation.
Curiosity Question:
Why does incrementing a pointer by 1 result in a change of 4 (or 8) bytes, and how does this relate to memory allocation in C++?
Dive deeper into pointer arithmetic for a better grasp of memory management in programming!
Additional Resources for Pointers in C++
- Article: Introduction to Pointers in C++
- Video Tutorial: Understanding Pointer Arithmetic in C++
- Book: "C++ Primer" by Stanley B. Lippman
Explore these resources to deepen your understanding of pointers and how arithmetic operations can be applied to pointer type variables in C++. Happy learning!
Practice
Task: Write a C++ program that creates an array, uses a pointer to traverse the array, and prints the values using pointer arithmetic.