Course: Programming Fundamentals (CS-303)
Topic: Pointer Arithmetic and Pointers with Arrays
Level: Beginner
Language: C++
By the end of this lecture, students will be able to:
In C++, pointers can be used in arithmetic operations to move between memory locations.
Unlike normal integer arithmetic, pointer arithmetic depends on the data type of the pointer.
For example:
int number = 10;
int *ptr = &number;
cout << ptr << endl;
cout << ptr + 1 << endl;
Explanation:
ptr stores the address of number.ptr + 1 moves the pointer to the next integer memory location.Important: Pointer arithmetic moves according to the size of the data type, not simply one byte.
C++ supports the following pointer arithmetic operations:
| Operation | Description |
|---|---|
ptr++ |
Move to the next element |
ptr-- |
Move to the previous element |
ptr + n |
Move forward by n elements |
ptr - n |
Move backward by n elements |
ptr2 - ptr1 |
Calculate the distance between two pointers |
#include <iostream>
using namespace std;
int main()
{
int num = 50;
int *ptr = #
cout << "Address: " << ptr << endl;
ptr++;
cout << "After increment: " << ptr << endl;
return 0;
}
Understand:
If int occupies 4 bytes, incrementing the pointer moves it forward by 4 bytes.
Note: In this example, incrementing the pointer moves it outside the valid object. We are demonstrating the arithmetic only; do not dereference it.
#include <iostream>
using namespace std;
int main()
{
int numbers[4] = {10, 20, 30, 40};
int *ptr = numbers;
cout << *ptr << endl;
ptr++;
cout << *ptr << endl;
return 0;
}
Output:
10
20
Explanation:
ptr = numbers stores the address of the first array element.*ptr accesses the first element.ptr++ moves the pointer to the next element.*ptr now accesses the second element.In C++, an array name usually behaves like a pointer to its first element when used in an expression.
Consider:
int numbers[5] = {10, 20, 30, 40, 50};
int *ptr = numbers;
Both expressions refer to the first element:
numbers
&numbers[0]
Therefore:
cout << numbers[0] << endl;
cout << *ptr << endl;
Both produce:
10
Pointer Explained at Margaret Burgin blog
Remember:
int *ptr = numbers;
is equivalent to:
int *ptr = &numbers[0];
However, an array name is not itself a modifiable pointer variable. You cannot write numbers++.
We can access array elements using pointer notation instead of array indexing.
Consider the following array:
int numbers[5] = {10, 20, 30, 40, 50};
| Array notation | Pointer notation | Value |
|---|---|---|
numbers[0] |
*(numbers + 0) |
10 |
numbers[1] |
*(numbers + 1) |
20 |
numbers[2] |
*(numbers + 2) |
30 |
numbers[3] |
*(numbers + 3) |
40 |
numbers[4] |
*(numbers + 4) |
50 |
Important Formula:
\[ numbers[i] = *(numbers+i) \]
Both notations access the same array element.
#include <iostream>
using namespace std;
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
cout << *(numbers + 0) << endl;
cout << *(numbers + 1) << endl;
cout << *(numbers + 2) << endl;
cout << *(numbers + 3) << endl;
cout << *(numbers + 4) << endl;
return 0;
}
Output:
10
20
30
40
50
We can use a pointer and a loop to access all elements of an array.
#include <iostream>
using namespace std;
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
int *ptr = numbers;
for (int i = 0; i < 5; i++)
{
cout << *ptr << endl;
ptr++;
}
return 0;
}
Output:
10
20
30
40
50
Dry Run:
| Iteration | Pointer position | *ptr |
Output |
|---|---|---|---|
| 1 | numbers[0] |
10 | 10 |
| 2 | numbers[1] |
20 | 20 |
| 3 | numbers[2] |
30 | 30 |
| 4 | numbers[3] |
40 | 40 |
| 5 | numbers[4] |
50 | 50 |
We can also perform calculations using pointers.
#include <iostream>
using namespace std;
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
int *ptr = numbers;
int sum = 0;
for (int i = 0; i < 5; i++)
{
sum = sum + *ptr;
ptr++;
}
cout << "Sum = " << sum << endl;
return 0;
}
Output:
Sum = 150
Explanation:
*ptr reads the current element.sum.ptr++ moves to the next element.| Array Indexing | Pointer Notation |
|---|---|
numbers[0] |
*numbers |
numbers[1] |
*(numbers + 1) |
numbers[2] |
*(numbers + 2) |
numbers[i] |
*(numbers + i) |
Both methods can access array elements.
Array indexing is generally easier to read, while pointer notation helps understand how arrays and memory addresses are related.
Pointers that point to elements of the same array can be compared.
Example:
#include <iostream>
using namespace std;
int main()
{
int numbers[4] = {10, 20, 30, 40};
int *ptr1 = &numbers[0];
int *ptr2 = &numbers[3];
if (ptr1 < ptr2)
{
cout << "ptr1 comes before ptr2";
}
return 0;
}
Output:
ptr1 comes before ptr2
ptr++ moves to the next element.ptr-- moves to the previous element.| Incorrect Code | Reason |
|---|---|
ptr = ptr + 1; when ptr is uninitialized |
Invalid pointer use |
*ptr++ without checking array bounds |
May dereference an invalid position |
numbers++ |
Array name cannot be modified |
ptr1 + ptr2 |
Adding two pointers is not allowed |
*(numbers + 5) for an array of size 5 |
Out-of-bounds access |
Write a C++ program that:
Write a C++ program that:
Write a C++ program that:
Write a C++ program that:
Write a C++ program that:
Q1. What does ptr++ do when ptr points to an integer array element?
Q2. What is the output?
int arr[3] = {10, 20, 30};
int *ptr = arr;
cout << *(ptr + 2);
Q3. Which expression is equivalent to arr[1]?
*(arr + 1)*(arr + 2)*arr + 1arr++Q4. What does ptr-- do?
Q5. Which operation is not allowed?
ptr++ptr + 2ptr - 1ptr1 + ptr2In this lecture, we learned:
Key Concepts to Remember
int *ptr = numbers;
*ptr // First element
*(ptr + 1) // Second element
*(ptr + 2) // Third element
ptr++; // Next element
ptr--; // Previous element
Remember: Array indexing and pointer notation can access the same array elements.