Pointer Arithmetic in C++
Pointer Arithmetic Program in C++.
1#include <iostream>2using namespace std;34int main() {5 int arr[] = {10, 20, 30, 40, 50};6 int* ptr = arr; // Points to first element78 cout << "Array elements using pointer arithmetic:" << endl;910 // Access elements using pointer11 for (int i = 0; i < 5; i++) {12 cout << "arr[" << i << "] = " << *(ptr + i) << endl;13 cout << "Address: " << (ptr + i) << endl;14 }1516 cout << "\nIncrementing pointer:" << endl;17 ptr = arr; // Reset to beginning18 for (int i = 0; i < 5; i++) {19 cout << "Value: " << *ptr << ", Address: " << ptr << endl;20 ptr++; // Move to next element21 }2223 cout << "\nDecrementing pointer:" << endl;24 ptr--; // Now points to last element25 for (int i = 0; i < 5; i++) {26 cout << "Value: " << *ptr << ", Address: " << ptr << endl;27 ptr--; // Move to previous element28 }2930 return 0;31}
Output
Array elements using pointer arithmetic: arr[0] = 10 Address: 0x7fff5fbff6a0 arr[1] = 20 Address: 0x7fff5fbff6a4 arr[2] = 30 Address: 0x7fff5fbff6a8 arr[3] = 40 Address: 0x7fff5fbff6ac arr[4] = 50 Address: 0x7fff5fbff6b0 Incrementing pointer: Value: 10, Address: 0x7fff5fbff6a0 Value: 20, Address: 0x7fff5fbff6a4 Value: 30, Address: 0x7fff5fbff6a8 Value: 40, Address: 0x7fff5fbff6ac Value: 50, Address: 0x7fff5fbff6b0 Decrementing pointer: Value: 50, Address: 0x7fff5fbff6b0 Value: 40, Address: 0x7fff5fbff6ac Value: 30, Address: 0x7fff5fbff6a8 Value: 20, Address: 0x7fff5fbff6a4 Value: 10, Address: 0x7fff5fbff6a0
What's going on
The program demonstrates pointer arithmetic operations:
Pointer arithmetic automatically accounts for data type size.
Header File Used
This header provides:
1#include <iostream>
Understanding Pointer Arithmetic
Arithmetic Concept
:
Key Rule
:
Adding to Pointers
Basic Addition
:
int arr[] = {10, 20, 30, 40, 50};
int* ptr = arr; // Points to arr[0]
*(ptr + 1) // Accesses arr[1] (value 20)
*(ptr + 2) // Accesses arr[2] (value 30)
How it works
:
Incrementing Pointers
Increment Operation
:
ptr++; // Moves to next element
Example
:
ptr = arr; // Points to arr[0]
ptr++; // Now points to arr[1]
ptr++; // Now points to arr[2]
How it works
:
Subtracting from Pointers
Basic Subtraction
:
*(ptr - 1) // Accesses previous element
Decrement Operation
:
ptr--; // Moves to previous element
How it works
:
Pointer Arithmetic with Different Types
int (4 bytes)
:
int* ptr;
ptr + 1 // Increases by 4 bytes
char (1 byte)
:
char* ptr;
ptr + 1 // Increases by 1 byte
double (8 bytes)
:
double* ptr;
ptr + 1 // Increases by 8 bytes
Key Point
: Arithmetic automatically accounts for type size.
Traversing Arrays
Using Pointer Arithmetic
:
int arr[] = {10, 20, 30, 40, 50};
int* ptr = arr;
for (int i = 0; i < 5; i++) {
}
1cout << *(ptr + i) << endl; // Access arr[i]
Using Increment
:
ptr = arr;
for (int i = 0; i < 5; i++) {
ptr++; // Move to next element
}
1cout << *ptr << endl;
When to Use Pointer Arithmetic
Best For
:
Example Scenarios
:
Important Considerations
Array Bounds
:
Type Safety
:
Pointer Comparison
:
return 0;
This ends the program successfully.