Skip to content
C++ Better Explained
Go back
How to Pass an Array to a Function in C++ (The Right Way)
Edit page

How to Pass an Array to a Function in C++

Passing an array to a function looks simple, but C++ does something surprising behind the scenes that catches almost every beginner: the array doesn’t go in as a whole array. Understand that one detail and array-and-function code stops being mysterious.


The Basic Syntax

Write the parameter with square brackets, and when you call the function, pass only the array’s name — no brackets:

#include <iostream>

double average(int arr[], int n) {
    int sum = 0;
    for (int i = 0; i < n; ++i)
        sum += arr[i];
    return static_cast<double>(sum) / n;
}

int main() {
    int scores[] = {80, 90, 100, 70};
    int n = sizeof(scores) / sizeof(scores[0]);
    std::cout << "Average: " << average(scores, n) << "\n";  // 85
    return 0;
}

Notice we pass n, the number of elements, as a second argument. That’s not optional bookkeeping — it’s required, and here’s why.


Why You Must Pass the Length

When an array is passed to a function, it decays into a pointer to its first element. The function never receives the array’s length, so a trick like sizeof(arr) / sizeof(arr[0]) would not work inside the function — it would measure the pointer, not the array.

That’s the reason for the extra int n. Without it, your loop has no idea where the array ends, and reading past the end is a common source of crashes and garbage values. Pass the length and your loop always stops in the right place.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Arrays Are Effectively Passed by Reference

Because the function holds a pointer to the original array, any change it makes is seen by the caller — there’s no copy. That’s different from passing a single int, which is copied. Watch this function change the caller’s data directly:

#include <iostream>

void doubleEach(int arr[], int n) {
    for (int i = 0; i < n; ++i)
        arr[i] *= 2;
}

int main() {
    int nums[] = {1, 2, 3};
    doubleEach(nums, 3);
    for (int i = 0; i < 3; ++i)
        std::cout << nums[i] << " ";   // 2 4 6
    std::cout << "\n";
    return 0;
}

This is powerful — and a little dangerous. If you don’t want a function to modify your array, mark the parameter const, like const int arr[], and the compiler will stop any accidental writes.


The Modern Alternative: std::vector

Raw arrays force you to juggle a separate length everywhere. A std::vector carries its own size, so you pass one thing and call .size() inside:

#include <iostream>
#include <vector>

double average(const std::vector<int>& v) {
    int sum = 0;
    for (int x : v)
        sum += x;
    return static_cast<double>(sum) / v.size();
}

int main() {
    std::vector<int> scores = {80, 90, 100, 70};
    std::cout << "Average: " << average(scores) << "\n";  // 85
    return 0;
}

Passing by const reference (const std::vector<int>&) avoids copying the whole list and promises the function won’t change it. For most beginner code, reaching for a vector is the cleaner choice.


Quick Reference

GoalHow
Pass a raw arrayvoid f(int arr[], int n) and call f(a, n)
Let the function edit itpass int arr[] (changes are visible)
Protect it from editspass const int arr[]
Avoid tracking lengthuse std::vector and .size()
Avoid copying a vectorpass const std::vector<int>&


Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 *Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Previous Post
Nested Loops in C++: Patterns, Grids, and the Multiplication Table
Next Post
How to Get a Substring in C++ with substr()