Pointers and arrays are closely related in C++. The name of an array acts like a pointer to its first element, which makes array handling more efficient and flexible.
What are Pointers with Arrays?
When an array is created, the array name stores the address of the first element. A pointer can be used to access and manipulate array elements.
Why Use Pointers with Arrays?
Pointers with arrays are useful because they:
- Allow efficient array traversal
- Improve performance
- Simplify memory handling
- Support dynamic programming techniques
Array Name as Pointer
int arr[5] = {10, 20, 30, 40, 50};
Here:
arr
stores the address of the first element.
Example Program
#include <iostream>
using namespace std;
int main() {
int arr[3] = {10, 20, 30};
cout << arr << endl;
cout << &arr[0] << endl;
return 0;
}
Both statements print the same address.
Using Pointer with Array
#include <iostream>
using namespace std;
int main() {
int arr[3] = {10, 20, 30};
int *ptr = arr;
cout << *ptr << endl;
return 0;
}
Output
10
Accessing Array Elements Using Pointer
#include <iostream>
using namespace std;
int main() {
int arr[3] = {10, 20, 30};
int *ptr = arr;
cout << *(ptr + 0) << endl;
cout << *(ptr + 1) << endl;
cout << *(ptr + 2) << endl;
return 0;
}
Output
10
20
30
Pointer Arithmetic with Arrays
Pointers move according to the size of the data type.
ptr++;
This moves the pointer to the next array element.
Traversing Array Using Pointer
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
for (int i = 0; i < 5; i++) {
cout << *(ptr + i) << endl;
}
return 0;
}
Modifying Array Elements Using Pointer
#include <iostream>
using namespace std;
int main() {
int arr[3] = {10, 20, 30};
int *ptr = arr;
*(ptr + 1) = 100;
cout << arr[1];
return 0;
}
Output
100
Difference Between Array and Pointer
| Array | Pointer |
|---|---|
| Fixed memory block | Variable storing address |
| Cannot be reassigned | Can point to different locations |
| Stores multiple elements | Stores memory address |
Important Points
- Array name acts like a constant pointer
- Pointers can access array elements efficiently
- Arrays and pointers are closely connected
- Pointer arithmetic is commonly used with arrays
Advantages of Using Pointers with Arrays
- Faster access to elements
- Efficient memory usage
- Useful in dynamic arrays
- Supports advanced data structures
Why Pointers with Arrays are Important
They are important because they:
- Improve array processing efficiency
- Support dynamic memory management
- Are used in low-level programming
- Help understand memory concepts deeply
Real-Life Example
Think of an apartment building:
- Array elements are apartments
- Pointer acts like a map pointing to apartment locations
Using pointer arithmetic is like moving from one apartment to the next.
Conclusion
Pointers with arrays in C++ provide efficient and flexible ways to access and manipulate array data. Understanding this relationship is essential for advanced programming, memory management, and high-performance applications.