In C++, an array is a collection of elements that are stored in a contiguous memory location and contain the same data type. In C++, we can pass the array to functions either by pointer or by reference. Arrays are mainly passed to functions to perform several operations on array elements efficiently, which helps to maintain the modular code.
In C++, if we want to reuse the array logic, we can create a function. We need to provide only the array name to pass an array to function in C++.
It has the following syntax:
Let's see an example of a C++ function that prints the array elements.
Output:
Printing array elements: 10 20 30 40 50 Printing array elements: 5 15 25 35 45
In C++, passing an array as a pointer is the most common and efficient method for passing an array to a function. In this method, we can pass the memory address of the initial array element that allows the function to handle arrays of the dynamic size.
It has the following syntax:
In this syntax,
Let us take an instance to demonstrate how we can pass an array as a pointer in C++.
Output:
Array elements are: 5 10 15 20 25 30
Explanation:
In this example, we have taken the int *arr in the display() function that receives the base address of the numbers array. After that, the array decays into a pointer, so the arr[i] function accesses elements via pointer arithmetic. Finally, we also pass the size to indicate the number of elements.
In C++, passing an array as a reference to a function is an important method that is mainly used to hold the size of the array and avoid the pointer decay. It represents the function that gets a reference to the original array. It enables the function to access and modify the actual elements of the array and hold its size details at compile time.
It has the following syntax:
Let us take an example to illustrate we can pass an array as a reference to a function in C++.
Output:
Array elements are: 12 25 16 30 34
Explanation:
In this example, we have taken the print_array() function, an array of 5 integers by reference, which ensures the retained array size. After that, it shows every array element using the for loop. Finally, this method prevents pointer decay and enables us to direct access to the original array.
In C++, passing as a sized array to a function is a common method to pass an array to a function. If we want to pass an array to a function, we can specify its size in the function's parameter list. It is known as passing a sized array. However, the array decays to a pointer when passed, which specifies the size that helps in accessing the correct number of elements.
It has the following syntax:
Let us take an instance to demonstrate how we can pass as a sized array to function in C++.
Output:
Elements in the array: 4 8 12 20 25
Explanation
In this example, we have taken the show() function that takes a fixed-size array of 5 integers, but the main function defines an array nums with 6 elements. In the show() function, the loop tries to access 6 elements, which leads to undefined behavior because arr[5] is outside the declared size of the parameter array.
In C++, passing as an unsized array to a function is similar to passing as a sized array. We can pass an unsized array to a function by avoiding the size in the parameter declaration.
It has the following syntax:
Let us take an instance to demonstrate how we can pass as an unsized array to function in C++.
Output:
Unsized Array elements are: 7 14 21 28 35
Explanation:
In this example, we have taken the show() function that accepts an unsized array (int arr[]) and its size as parameters. After that, the array nums is passed with its length, which allows the function to iterate through and display each element.
Let us take an instance to demonstrate how we can pass a multidimensional array to a function in C++.
Output:
Elements of Matrix are: 9 8 7 6 5 4
Explanation:
In this example, we have taken a 3×2 two-dimensional array that is passed to the printMatrix function. After that, the function takes a fixed-size 2D array and shows all its elements via nested loops.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India