Aug 28, 2026 — Digital Edition
Bradley Ideas
Independent Journalism & Insight
Feature

Pass Array to Function C++

Passing arrays to functions in C++ is a common yet potentially confusing task. Arrays are unlike other data types in C++, and their peculiarities can trip up even seasoned developers. Let's explore how to pass arrays to functions in C++.

How to Print 2D Array in C
How to Print 2D Array in C

Before we dive into the code, it's crucial to understand that there are two primary ways to pass arrays to functions: by reference and by value. Each method has its own use cases and specificities, so let's explore them one by one.

Array vs Pointer in C 😳 (Simple Beginner Guide)
Array vs Pointer in C 😳 (Simple Beginner Guide)

Pass by Value

In C++, arrays by default are passed by reference, which means the function receives a pointer to the array's memory location. However, there's a way to pass arrays by value, which involves décorating them as function arguments.

Pass Array to Functions in C - GeeksforGeeks
Pass Array to Functions in C - GeeksforGeeks

Passing by value allows the function to manipulate the array as if it were local data, with no side effects on the original array. This is particularly useful when working with large arrays as it avoids potential memory overhead. However, note that function calls that accept arrays by value may be less readable and harder to optimize by the compiler.

Décorating Arrays as Function Arguments

C Arrays Cheat Sheet (1D, 2D Arrays, Syntax, Examples & Programs)
C Arrays Cheat Sheet (1D, 2D Arrays, Syntax, Examples & Programs)

To pass an array by value to a C++ function, you need to specify its size using the array decorator. This decorator helps the function understand the array's size, ensuring it can access all elements even if they're not contiguous in memory.

Here's an example: ```cpp void printArray(int arr[5]) { for (int i = 0; i < 5; i++) { std::cout << arr[i] << " "; } } int main() { int arr[5] = {1, 2, 3, 4, 5}; printArray(arr); // arr is decorated as int[5] return 0; } ```

Using the sizeof Operator

Arrays in Embedded C: Complete Guide with Examples
Arrays in Embedded C: Complete Guide with Examples

An alternative way to pass an array by value is by using the `sizeof` operator. This method is particularly useful when you're working with dynamically allocated arrays or don't know the array's size upfront. The `sizeof` operator returns the size of the array in bytes, which you can then use to determine the array's size.

Here's an example: ```cpp void printArray(int* arr, int size) { for (int i = 0; i < size; i++) { std::cout << arr[i] << " "; } } int main() { int arr[] = {1, 2, 3, 4, 5}; printArray(arr, sizeof(arr) / sizeof(arr[0])); // arr is passed by value return 0; } ```

Pass by Reference

Array Data Structure - GeeksforGeeks
Array Data Structure - GeeksforGeeks

As mentioned earlier, arrays in C++ are typically passed by reference to functions by default. This means the function receives a pointer to the array's first element, which it can use to access and modify the array's contents.

Passing by reference is more efficient in terms of memory usage, as no copy of the array is made. However, it also means that the function can modify the original array, which may or may not be the desired behavior.

C++
C++
a screenshot of a computer screen with some type of program in the bottom right corner
a screenshot of a computer screen with some type of program in the bottom right corner
Computer Coding, Data Science, Computer Science, Helping People, Science, Coding
Computer Coding, Data Science, Computer Science, Helping People, Science, Coding
an array of different types of fruits on a yellow background with the words arrays
an array of different types of fruits on a yellow background with the words arrays
an image of a computer screen with numbers and symbols on it, all in different colors
an image of a computer screen with numbers and symbols on it, all in different colors
Vector vs Array in C++ | STL Vector vs Array Differences Explained
Vector vs Array in C++ | STL Vector vs Array Differences Explained
an image of a computer screen with text and numbers on the page, which reads 12 ways to create a function
an image of a computer screen with text and numbers on the page, which reads 12 ways to create a function
C++ tip 13: Prefer pass-by-reference-to-const to pass-by-value — Harold Serrano - Game Engine Developer
C++ tip 13: Prefer pass-by-reference-to-const to pass-by-value — Harold Serrano - Game Engine Developer
Array in C++ Handwritten Notes 📘 | Easy Array Notes for Beginners
Array in C++ Handwritten Notes 📘 | Easy Array Notes for Beginners

Array Decaying

When you pass an array to a function, it decays to a pointer to its first element. This behavior can sometimes lead to confusion, as it means that functions that expect array arguments actually receive pointers. To illustrate this, consider the following example: ```cpp void printArray(int arr[]) { for (int i = 0; i < 5; i++) { std::cout << arr[i] << " "; } } int main() { int arr[5] = {1, 2, 3, 4, 5}; printArray(arr); return 0; } ```

In this example, the `printArray` function expects an array as an argument. However, when you call it with the `arr` variable, it actually receives a pointer to the first element of the array. This is why the function can access all elements of the array using the pointer.

Passing Multidimensional Arrays

Passing multidimensional arrays to functions in C++ follows the same rules as passing one-dimensional arrays. However, you need to specify the size of all but the first dimension when decorating the array as a function argument. This is because C++ only checks the first dimension's size for arrays passed by reference.

Here's an example: ```cpp void print2DArray(int arr[3][4]) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { std::cout << arr[i][j] << " "; } std::cout << "\n"; } } int main() { int arr[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; print2DArray(arr); // arr is decorated as int[3][4] return 0; } ```

In conclusion, understanding how to pass arrays to functions in C++ is crucial for writing efficient and maintainable code. While arrays can behave unexpectedly at times, mastering their peculiarities will make you a better C++ developer. Keep practicing and experimenting with different array usage scenarios, and you'll soon become proficient in array manipulation.