Pointer Arithmetic
Pointer Arithmetic Program in C++
C++ Pointer Arithmetic Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
int* ptr = arr; // Points to first element
cout << "Array elements using pointer arithmetic:" << endl;
// Access elements using pointer
for (int i = 0; i < 5; i++) {
cout << "arr[" << i << "] = " << *(ptr + i) << endl;
cout << "Address: " << (ptr + i) << endl;
}
cout << "\nIncrementing pointer:" << endl;
ptr = arr; // Reset to beginning
for (int i = 0; i < 5; i++) {
cout << "Value: " << *ptr << ", Address: " << ptr << endl;
ptr++; // Move to next element
}
cout << "\nDecrementing pointer:" << endl;
ptr--; // Now points to last element
for (int i = 0; i < 5; i++) {
cout << "Value: " << *ptr << ", Address: " << ptr << endl;
ptr--; // Move to previous element
}
return 0;
}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
Understanding Pointer Arithmetic
This program teaches you how to perform Pointer Arithmetic in C++. Pointer arithmetic allows you to perform operations like addition and subtraction on pointers. When you add or subtract integers from pointers, the address changes by the size of the data type, making it perfect for traversing arrays.
---
1. What This Program Does
The program demonstrates pointer arithmetic operations:
Pointer arithmetic automatically accounts for data type size.
---
2. Header File Used
This header provides:
---
#include <iostream>3. Understanding Pointer Arithmetic
Arithmetic Concept
:
Key Rule
:
---
4. 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
:
---
5. 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
:
---
6. Subtracting from Pointers
Basic Subtraction
:
*(ptr - 1) // Accesses previous element
Decrement Operation
:
ptr--; // Moves to previous element
How it works
:
---
7. 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.
---
8. Traversing Arrays
Using Pointer Arithmetic
:
int arr[] = {10, 20, 30, 40, 50};
int* ptr = arr;
for (int i = 0; i < 5; i++) {
}
cout << *(ptr + i) << endl; // Access arr[i]Using Increment
:
ptr = arr;
for (int i = 0; i < 5; i++) {
ptr++; // Move to next element
}
---
cout << *ptr << endl;9. When to Use Pointer Arithmetic
Best For
:
Example Scenarios
:
---
10. Important Considerations
Array Bounds
:
Type Safety
:
Pointer Comparison
:
---
11. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning efficient array traversal, understanding memory layout, and preparing for advanced pointer operations in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Pointer Arithmetic
This C++ program is part of the "Memory Management Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.