How to Pass Arrays to Functions in C++

How to Pass Arrays to Functions in C++

software-development#cpp#coding#programming

The moment it clicked

Lets deep dive into c++ and one concept that trips up many beginners (including me once) is passing arrays to functions.

I made a YouTube video about this a while back. You can watch it here:

👉 Passing Arrays to Functions in C++

Let me walk you through the exact code I used — because seeing it in action makes all the difference.


The Code That Made Sense

Here's the full example:


// functions:

#include <iostream>

#include <fstream>

#include <cstdlib>

using namespace std;

  

const int SIZE = 3;

  

void inputArray(int (&arrayP)[]) {

cout << "Enter " << SIZE << " values: " << endl;

	for (int i = 0; i < SIZE; i++) {

		cin >> arrayP[i];

	}

}

  

void outputArray(const int arrayP[]) {

		cout << "The values are : " << endl;

		for (int i = 0; i < SIZE; i++) {

			cout << arrayP[i] << ' ';

			cout << endl;

		}

}

  

int main() {

	int array[SIZE];

	inputArray(array);

	outputArray(array);

	return 0;

}

Breaking It Down

void inputArray(int (&arrayP)[])

This looks weird at first — (&arrayP)[] means we're passing the array by reference.

Why? In C++, if you just write int arrayP[] as a parameter, the array decays into a pointer. That still works, but you lose the original array's size information. Using reference syntax (&arrayP)[] keeps it as a true array reference. It's a cleaner way to say: "This function can modify the original array."

Inside, we read SIZE values directly into arrayP[i] — and because it's a reference, it changes the original array in main().

void outputArray(const int arrayP[])

Here, const is your friend. It promises: "This function will NOT modify the array."

Even though int arrayP[] looks like an array, it's actually a pointer to the first element. But the const makes sure you don't accidentally write to it.

Try changing arrayP[i] = 99; inside outputArray — the compiler will stop you. That's a good thing. It prevents bugs.

  1. Why const matters in functions

Readability — Anyone reading your code knows outputArray is read-only.

Safety — You can't accidentally modify data.

Flexibility — You can pass both const and non-const arrays to a const parameter.

A Quick Note on SIZE

I used:


const int SIZE = 3;

This is a global constant. In a bigger program, you'd probably pass the size as another parameter. But for learning, a constant size keeps things simple.

What About the YouTube Video?

The video shows me live-debugging array passing and explaining why (&arrayP)[] works. It includes a visual diagram of memory — because sometimes you need to see that the function isn't copying the whole array, just referencing it.

Final Takeaway

Passing arrays to functions in C++ feels weird at first because arrays "decay" into pointers. But once you understand:

Arrays are passed by reference (through the & syntax) when you want to modify them.

Use const for read‑only access.

The array name in a parameter is secretly a pointer (except when you use (&name)[]).

…it stops being magic and starts being useful.

Now go pass some arrays.



Share this post:

About the author