1. Intro
In C++, you often need to pass an array to a function — for example, to compute the sum of its elements or sort its contents. Unlike ordinary variables, arrays behave very differently when passed as function arguments, and understanding this behavior is essential to avoid subtle bugs.
Cricket analogy: Handing a scorer the full over-by-over ledger to compute the total, rather than just telling them one number, is like passing an array to a function — the whole structure, not a single copied value, is what gets worked with.
2. Syntax
// The array parameter and a separate size parameter
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
}
// Called as:
int numbers[5] = {1, 2, 3, 4, 5};
printArray(numbers, 5);3. Explanation
When an array is passed to a function, it decays to a pointer to its first element — int arr[] in the parameter list is really treated as int* arr. This means the function does NOT automatically know the array's size; sizeof(arr) inside the function would give the size of a pointer, not the size of the original array. Because of this, the caller must always pass the size as a separate parameter, as shown above with int size.
Cricket analogy: Handing someone just the bat used to start an innings, a pointer, doesn't tell them how many overs were bowled — you must separately announce 'this was a 50-over innings', just as an array decays to a pointer and the function needs the size passed explicitly.
**Array decay:** an array argument decays to a pointer to its first element, so size information is lost. Always pass the length as an extra parameter alongside the array.
This decay behavior has an important side effect that contrasts sharply with C++'s normal pass-by-value default: for ordinary variables (like int), a function receives a copy, so modifying the parameter inside the function does not affect the caller's original variable. Arrays behave differently — because the function only ever receives a pointer to the original array's memory, any changes made to arr[i] inside the function directly modify the caller's original array. In other words, arrays are effectively passed by reference to their contents, even though the syntax looks like ordinary pass-by-value.
Cricket analogy: Giving a junior scorer a photocopy of the scoresheet to mark up, pass-by-value, leaves the master sheet untouched, but handing them the actual master ledger of the innings, an array, means their edits directly change the real record — arrays are effectively passed by reference to their contents.
4. Example
#include <iostream>
using namespace std;
void doubleValues(int arr[], int size) {
for (int i = 0; i < size; i++) {
arr[i] = arr[i] * 2;
}
}
int main() {
int numbers[4] = {1, 2, 3, 4};
doubleValues(numbers, 4);
for (int i = 0; i < 4; i++) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}5. Output
2 4 6 8 6. Key Takeaways
- An array passed to a function decays to a pointer to its first element — size information is lost.
- The array's size must always be passed separately as an extra parameter.
- Unlike normal pass-by-value variables, changes to array elements inside the function DO affect the caller's original array.
sizeof(arr)inside a function that received an array parameter gives the pointer size, not the array's total size.
Practice what you learned
1. What happens to an array when it is passed as an argument to a function in C++?
2. Why must a function that receives an array also receive the array's size as a separate parameter?
3. If a function modifies `arr[0]` where `arr` is an array parameter, what happens to the caller's original array?
4. How does passing an array to a function differ from passing an `int` by value?
5. Inside a function `void f(int arr[], int size)`, what does `sizeof(arr)` return?
Was this page helpful?
You May Also Like
Arrays in C++
Learn what an array is in C++, how to declare and access a fixed-size collection of same-type elements, and why zero-based indexing matters.
Pointers and Arrays in C++
See how an array name decays into a pointer to its first element, and how `arr[i]` and `*(arr + i)` are two ways of writing the same thing.
Function Parameters and Arguments in C++
Learn the difference between function parameters and arguments in C++ and understand pass-by-value semantics with a hands-on example.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics