In C++, the relationship between pointers, arrays, and functions is so intimate that they are often theoretically inseparable. To master C++, one must understand how arrays "decay" into pointers and how functions use this to manipulate large sets of data efficiently.
Theoretically, the name of an array is a constant pointer to its first element.
When you use an array name in an expression (like passing it to a function), it "decays" into a pointer to its first element.
If you have int arr[5], the identifier arr is equivalent to &arr[0].
Because of this, the compiler treats arr[i] and *(arr + i) as exactly the same operation.
In a function parameter list, the following three declarations are identical to the compiler:
void func(int* p);
void func(int p[]);
void func(int p[10]); // The '10' is ignored by the compiler!
When you pass an array to a function, you are technically performing a Call by Reference (via a pointer).
Efficiency: You are not copying the entire array (which could be millions of items). You are only copying the address of the first item (typically 8 bytes).
Side Effects: Because the function receives the actual memory address, any changes made to the array elements inside the function will change the original array in the caller's scope.
One of the most dangerous aspects of pointers with arrays is the loss of size information.
When an array is passed to a function, it loses its "array-ness" and becomes a simple pointer. Inside the function, if you try to use sizeof(arr), it will return the size of the pointer (usually 8 bytes), NOT the size of the whole array.
The Solution: You must always pass a second argument to the function: the Size. Without the size, the function has no theoretical way of knowing where the array ends, leading to potential buffer overflows.
Pointers don't just point to data; they can point to code. A Function Pointer stores the starting address of the executable code of a function.
Usage: This allows you to pass a function as an argument to another function. This is the basis of "Plug-and-Play" code or "Callbacks."
Example: You could write a sort function and pass it a pointer to a comparison function. This allows the same sort code to sort numbers in ascending or descending order depending on which function pointer you provide.
This program demonstrates passing an array to a function via a pointer and modifying the original values.
#include <iostream> using namespace std; // Function takes a pointer to the array and its size // Note: 'int* arr' is the same as 'int arr[]' void doubleElements(int* arr, int size) { for (int i = 0; i < size; i++) { // Using pointer arithmetic to modify original data *(arr + i) *= 2; } } void printArray(const int* arr, int size) { for (int i = 0; i < size; i++) { cout << arr[i] << " "; // Standard indexing works on pointers too } cout << endl; } int main() { int myNumbers[] = {1, 2, 3, 4, 5}; int n = sizeof(myNumbers) / sizeof(myNumbers[0]); cout << "Original array: "; printArray(myNumbers, n); // Pass the array name (it decays to a pointer) doubleElements(myNumbers, n); cout << "After doubling: "; printArray(myNumbers, n); return 0; }
| Scenario | Theoretical Behavior |
| Passing arr | Passes the address of the first element (&arr[0]). |
| Function Modification | Changes are permanent (Call by Reference). |
| sizeof inside function | Returns pointer size, not array size. |
| *(arr + i) | Accesses the $i$-th element via pointer arithmetic. |
| const int* arr | Passes the array by reference but prevents modification. |
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION