Stage 11: Pointer Arithmetic in C++
Understanding how C++ operates on memory addresses. Learn why ptr++ is more than just adding one.
Pointer Arithmetic: Navigating Through Memory
Pointer arithmetic is a mechanism that allows you to perform addition and subtraction on memory addresses. It is the foundation of how arrays work in C++. The key rule to remember for your exam is that these operations are scaled by the size of the data type the pointer points to.
1. Incrementation and Decrementation (ptr++, ptr--)
When you increment a pointer by 1 (ptr++), you are not moving by one byte in memory. You move by as many bytes as the data type occupies (i.e., by sizeof(type)).
- If an
inttakes 4 bytes,ptr++increases the address by 4. - If a
doubletakes 8 bytes,ptr++increases the address by 8.
int arr[] = {10, 20, 30};
int *p = arr; // points to arr[0]
p++; // p now points to arr[1]
std::cout << *p; // Prints 202. Adding and Subtracting Integers
You can move a pointer by any number of elements by adding or subtracting an integer.
int *p = &arr[0];
int *p2 = p + 2; // p2 now points to arr[2]3. Subtracting Pointers
If you have two pointers pointing to elements of the same array, you can subtract them. The result is not the number of bytes, but the number of elements between them.
int *p_start = &arr[0];
int *p_end = &arr[2];
int distance = p_end - p_start; // Result: 24. Relationship Between Pointers and Arrays
In C++, the arr[i] notation is just "syntactic sugar" for a pointer operation. The compiler always translates it to:
*(arr + i)
This means you can iterate through an array using just the pointer, which is often required in low-level tasks.
const int N = 3;
int numbers[N] = {100, 200, 300};
int *ptr = numbers;
for(int i = 0; i < N; i++) {
std::cout << *(ptr + i) << " "; // Same as ptr[i] or numbers[i]
}Exam Pitfalls
- Multiplication and Division: You cannot multiply or divide pointers. Operations like
ptr * 2will cause a compilation error. - Adding Pointers: You cannot add a pointer to another pointer (
ptr1 + ptr2). It makes no logical sense. - Out of Bounds: Arithmetic allows you to set a pointer outside the array. C++ won't stop you, but attempting to dereference (
*) such an address will crash the program. - void Pointers*: You cannot perform arithmetic on
void*pointers because the compiler doesn't know the size to shift the address by.
Example: Reversing an Array with Pointers
void reverse(int *left, int *right) {
while (left < right) {
int temp = *left;
*left = *right;
*right = temp;
left++;
right--;
}
}You might also like
Stage 10: Pointers, Address-of, and Dereference Operators
Understanding the foundations of memory addressing in C++: how pointers and low-level operators work.
Stage 3: Automatic One-Dimensional Arrays in C++
A guide to static one-dimensional arrays: declaration, initialization, and safe iteration.
Stage 6: C-Style String Handling (char[])
A guide to low-level string processing as null-terminated character arrays, following exam constraints.