Cpp Pointer To An Array
## Understanding Pointers to Arrays in C++
In C++, there is a close and powerful relationship between pointers and arrays. Understanding how they interact is fundamental to mastering memory management and performance optimization in C++.
---
### Array Names as Pointers
In C++, the name of an array acts as a constant pointer to the very first element of the array.
Consider the following array declaration:
```cpp
double balance;
```
Here, `balance` is a constant pointer to `&balance`, which is the memory address of the first element in the array.
Because the array name decays to a pointer to its first element, you can assign the array name directly to a pointer variable of the matching type:
```cpp
double *p;
double balance;
// Assign the address of the first element to p
p = balance;
```
Both `balance` and `p` now point to the same memory location. However, there is a key difference: **`p` is a variable pointer** (you can change the address it points to), whereas **`balance` is a constant pointer** (you cannot reassign it to point elsewhere).
---
### Accessing Array Elements via Pointer Arithmetic
Because array elements are stored in contiguous memory locations, you can use pointer arithmetic to traverse and access array data.
If `p` points to the first element of an array, then:
* `*p` or `*(p + 0)` accesses the first element (`array`).
* `*(p + 1)` accesses the second element (`array`).
* `*(p + 2)` accesses the third element (`array`), and so on.
Similarly, you can use pointer arithmetic directly with the array name:
* `*(balance + 4)` is a perfectly valid way to access the data stored at `balance`.
---
### Complete Code Example
The following program demonstrates how to access array elements using both a pointer variable (`p`) and the array name (`balance`) as a pointer.
```cpp
#include
using namespace std;
int main ()
{
// An array of doubles containing 5 elements
double balance = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p;
// Assign the address of the first element to the pointer
p = balance;
// Output each element's value using the pointer variable
cout << "Array values using pointer variable (p):" << endl;
for ( int i = 0; i < 5; i++ )
{
cout << "*(p + " << i << ") : " << *(p + i) << endl;
}
// Output each element's value using the array name as a constant pointer
cout << "\nArray values using array name (balance) as address:" << endl;
for ( int i = 0; i < 5; i++ )
{
cout << "*(balance + " << i << ") : " << *(balance + i) << endl;
}
return 0;
}
```
#### Output
When the above code is compiled and executed, it produces the following output:
```text
Array values using pointer variable (p):
*(p + 0) : 1000
*(p + 1) : 2
*(p + 2) : 3.4
*(p + 3) : 17
*(p + 4) : 50
Array values using array name (balance) as address:
*(balance + 0) : 1000
*(balance + 1) : 2
*(balance + 2) : 3.4
*(balance + 3) : 17
*(balance + 4) : 50
```
---
### Key Considerations
1. **Constant vs. Non-Constant Pointers**:
While you can perform operations like `p++` (which increments the pointer to point to the next element), you **cannot** perform `balance++`. The array name `balance` is a constant pointer bound to the array's starting address, and its address cannot be modified.
2. **Type Safety**:
A pointer to an array element must match the data type of the array. For example, a `double` array requires a pointer of type `double*`. The compiler uses this type information to determine how many bytes to offset the address during pointer arithmetic (e.g., adding `1` to a `double*` advances the address by `8` bytes on most modern systems).
3. **Out-of-Bounds Risks**:
C++ does not perform bounds checking on arrays or pointers. If you increment a pointer beyond the array's size (e.g., accessing `*(p + 5)` on a 5-element array), you will access undefined memory, leading to unpredictable behavior or segmentation faults.
YouTip