The short answer
When an array is passed to a function, what is actually passed is the base address: the address of the array’s first element. The function does not get a copy of every element, and it does not get the value of the first element by itself. It gets a pointer that tells it where the array starts.
What the base address is
An array stores its elements one after another in memory. The base address is the address of element zero, and every other element is found by counting forward from it.
When you use an array name in most expressions, the compiler treats it as a pointer to the first element. This is called array-to-pointer conversion, and it is sometimes called decay. When you pass numbers to a function, the value that goes into the call is that pointer.
The program below prints the addresses so you can compare them:
#include <iostream>
void showAddress(const int values[]) {
std::cout << "values in function: " << static_cast<const void *>(values) << '\n';
}
int main() {
int numbers[] = {3, 7, 11, 15};
std::cout << "numbers: " << static_cast<const void *>(numbers) << '\n';
std::cout << "&numbers[0]: " << static_cast<const void *>(&numbers[0]) << '\n';
showAddress(numbers);
return 0;
}
All three lines show the same address. The hexadecimal value changes from run to run, but the three values match within a single run.
The parameter is a pointer
Inside a parameter list, the compiler adjusts an array parameter to a pointer. These two declarations mean the same thing:
void showAddress(const int values[]);
void showAddress(const int *values);
Readers who ask “What are * and & in C?” will recognize the star in the second line as a pointer. The question “Is char[] and char * the same in C?” has the same answer here: in a parameter list, char s[] and char *s are both pointers, and the same reasoning holds for arrays of int.
Because the function receives only the pointer, it works on the caller’s data directly. The pointer is a small value, often 8 bytes on a 64-bit system, so passing it is cheap. The elements themselves are not copied.
Why the other answers are wrong
Multiple-choice versions of this question list four options: all elements, first element only, base address, and nothing. Here is why only one fits:
- All elements. A built-in array can’t be passed by value in C or C++, so the elements are not copied.
- First element only. This is close, but the function doesn’t receive the value of the first element. It receives where that element lives.
- Base address. This is correct: the address of the first element.
- Nothing. The function clearly receives something it can use to read the array.
Why the function can’t find the length
Since the function receives only a pointer, it has no built-in way to know how many elements follow. The sizeof operator shows this clearly:
#include <iostream>
void showSize(const int values[]) {
std::cout << "inside the function: " << sizeof(values) << '\n';
}
int main() {
int numbers[] = {3, 7, 11, 15};
std::cout << "in main: " << sizeof(numbers) << '\n';
showSize(numbers);
return 0;
}
On a typical 64-bit system, where int is 4 bytes and a pointer is 8 bytes, the output is:
in main: 16
inside the function: 8
Inside showSize, values is a pointer, so sizeof(values) gives the size of the pointer, not the array. The element count has to be passed as a separate argument. For the full syntax, see how to pass an array to a function in c.
What this means in practice
- Changes made to elements inside the function are visible to the caller, because both refer to the same memory.
- The function needs the element count as a separate argument.
- Mark the parameter
constwhen the function only reads the data.