100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Passing Arrays to Functions in C++

Understand how arrays decay to pointers when passed to C++ functions, why size must be passed separately, and how this differs from pass-by-value.

ArraysIntermediate7 min readJul 7, 2026
Analogies

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

cpp
// 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

cpp
#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

text
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

Was this page helpful?

Topics covered

#CStudyNotes#Programming#PassingArraysToFunctionsInC#Passing#Arrays#Functions#Syntax#DataStructures#StudyNotes#SkillVeris