Array Names as Pointers
In C++, array names behave like pointers to the first element. Consider this example:
#include <iostream>
using namespace std;
int main() {
double values[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double* ptr;
ptr = values;
cout << "ptr address: " << ptr << "\t" << "values address: " << values << endl << endl;
cout << "Accessing via pointer:" << endl;
for (int i = 0; i < 5; i++) {
cout << "*(ptr + " << i << "): " << *(ptr + i) << endl;
cout << "ptr[" << i << "]: " << ptr[i] << endl;
}
cout << endl << "Accessing via array name:" << endl;
for (int i = 0; i < 5; i++) {
cout << "*(values + " << i << "): " << *(values + i) << endl;
cout << "values[" << i << "]: " << values[i] << endl;
}
return 0;
}
Output:
ptr address: 0x6ffde0 values address: 0x6ffde0
Accessing via pointer:
*(ptr + 0): 1000
ptr[0]: 1000
*(ptr + 1): 2
ptr[1]: 2
*(ptr + 2): 3.4
ptr[2]: 3.4
*(ptr + 3): 17
ptr[3]: 17
*(ptr + 4): 50
ptr[4]: 50
Accessing via array name:
*(values + 0): 1000
values[0]: 1000
*(values + 1): 2
values[1]: 2
*(values + 2): 3.4
values[2]: 3.4
*(values + 3): 17
values[3]: 17
*(values + 4): 50
values[4]: 50
Both ptr and values point to the same memory address 0x6ffde0. The type of values is double*. You can use either pointer notation *(ptr + i) or array notation ptr[i] to access elements through either variable.
Passing Arrays to Functions
Since array names are pointers, you pass the pointer to the function. Three equivalent parameter declarations exist:
#include <iostream>
using namespace std;
void display1(double* data, int count);
void display2(double data[], int count);
void display3(double data[5], int count);
int main() {
double numbers[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
cout << "numbers address: " << numbers << endl << endl;
display1(numbers, 5);
display2(numbers, 5);
display3(numbers, 5);
return 0;
}
void display1(double* data, int count) {
cout << "display1(double* data, int count)" << endl;
for (int i = 0; i < count; i++) {
cout << data[i] << " ";
}
cout << endl;
}
void display2(double data[], int count) {
cout << "display2(double data[], int count)" << endl;
for (int i = 0; i < count; i++) {
cout << data[i] << " ";
}
cout << endl;
}
void display3(double data[5], int count) {
cout << "display3(double data[5], int count)" << endl;
for (int i = 0; i < count; i++) {
cout << data[i] << " ";
}
cout << endl;
}
Output:
numbers address: 0x6ffdf0
display1(double* data, int count)
1000 2 3.4 17 50
display2(double data[], int count)
1000 2 3.4 17 50
display3(double data[5], int count)
1000 2 3.4 17 50
All three declarations compile identically. The compiler treats double data[] and double data[5] as double* after processing function parameters.
Returning Arrays from Funcitons
To return an array from a function, return a pointer to the array's first element. However, local arrays are destroyed when the function exits. To preserve data beyond function scope, declare the array as static:
#include <iostream>
using namespace std;
double* createArrayNormal();
double* createArrayStatic();
int main() {
double* result;
result = createArrayNormal();
cout << "Returned from createArrayNormal() at address " << result << endl;
cout << "Local array: double arr[5] = {6.0, 6.1, 6.2, 6.3, 6.4};" << endl;
for (int i = 0; i < 5; i++) {
cout << "result[" << i << "]: " << result[i] << endl;
}
cout << endl;
result = createArrayStatic();
cout << "Returned from createArrayStatic() at address " << result << endl;
cout << "Static array: static double arr[5] = {6.0, 6.1, 6.2, 6.3, 6.4};" << endl;
for (int i = 0; i < 5; i++) {
cout << "result[" << i << "]: " << result[i] << endl;
}
return 0;
}
double* createArrayNormal() {
double arr[5] = {6.0, 6.1, 6.2, 6.3, 6.4};
cout << "Local arr address: " << arr << endl;
return arr;
}
double* createArrayStatic() {
static double arr[5] = {6.0, 6.1, 6.2, 6.3, 6.4};
cout << "Static arr address: " << arr << endl;
return arr;
}
Output:
Local arr address: 0x6ffda0
Returned from createArrayNormal() at address 0x6ffda0
Local array: double arr[5] = {6.0, 6.1, 6.2, 6.3, 6.4};
result[0]: 6.12209e-317
result[1]: 4.94066e-324
result[2]: 2.17389e-322
result[3]: 6.12209e-317
result[4]: 3.62622e-317
Static arr address: 0x472020
Returned from createArrayStatic() at address 0x472020
Static array: static double arr[5] = {6.0, 6.1, 6.2, 6.3, 6.4};
result[0]: 6
result[1]: 6.1
result[2]: 6.2
result[3]: 6.3
result[4]: 6.4
The non-static version returns garbage values because the local array is deallocated when the function returns. The static version preserves the array for the entire program lifetime, allowing correct access after function exit.
Important Note
The sizeof operator behaves differently for array names versus pointers. An array name yields the total byte size of the array, while a pointer variable yields only the pointer's own size (typically 4 or 8 bytes on 32/64-bit systems).