Passing Arrays to Functions in C++
In the C++ programming language, array data elements can be passed or moved to user-defined functions in multiple orders or steps. Array data values can be passed to custom array functions by indicating their exact array size in the user-defined custom function signature definition with special pointer operators, or by-passing array data values with their default storage size to custom array functions. When C++ users manually pass array data values to a custom function, they are passing a pointer to the first data element of the actual array.

So, let’s take a closer look at how arrays are passed to functions in C++ programming.
When C++ users pass array data values to a function program, they are not passing the entire array, but a pointer to the first element of the existing user-defined array. This means that the actual original array data type value can be modified in the existing function. Unless the existing array data type is passed as a constant with the const keyword.
Steps for passing an array to a function in C++ programming.
- Passing array element values by reference to an existing array as the default method.
- Passing array values with information about the array data type size.
- Passing array data values to a data type using special pointer operators.
- Passing array element values with a declared constant array data type size.
So, let’s learn about references, sizes, pointers, and passing arrays to functions with constant data sizes in the C++ programming language.
Passing array value data to a function by reference in C++ is the default concept.
In the default order in a C++ program, when an array data type value is passed or moved to a function, it is passed as a reference to the first array element (a pointer operator). In this process the function may modify or update your real original array.
Example of passing array elements to a C++ function.
#include <iostream>
using namespace std;
void updateArray(int array[], int size) {
for (int p = 0; p < size; p++) {
array[p] += 3; // here it adds 3 with each element in the current array
}
}
int main() {
int array[7] = {9, 7, 10, 2, 11, 13, 17};
// here it Pass the array to the function value
updateArray(array, 7);
// here it Print the modified array element in the console screen
for (int p = 0; p < 7; p++) {
cout << array[p] << ” “; // Result is – 12 10 13 5 14 16 20
}
cout << endl;
return 0;
}
Explanation of passing array elements to a C++ function.
- In this example, when an array element is passed to updateArray, the current function automatically receives a pointer to the first element of the array. Because of this, any modifications within updateArray affect the actual original array in the main function.
Passing array elements with array size information in C++.
A basic method in C++ programming is to pass both the array data type and the declared array’s default element storage size to a function. Remember, C++ programming does not actually store the default size of the array. Therefore, the C++ user must pass the defined array size in an explicit order.
Example of passing an array element with the size.
#include <iostream>
using namespace std;
void displayArray(int array[], int size) {
for (int p = 0; p < size; p++) {
cout << array[p] << “
“; // Here it prints each element in the current array size
}
cout << endl;
}
int main() {
int array[7] = {98, 77, 33, 74, 99, 100, 130};
displayArray(array, 7); // Here it passes the array and its element size one by one
return 0;
}
Explanation of passing an array element with the size.
- In this example, the user-defined function displayArray accepts both array (array[]) and the size of the array (size) as arguments. And in this process the displayArray function is able to iterate over the array in order.
Passing Array Elements Using Pointers in C++.
C++ users can pass array elements to functions using special pointer memory address operators. In fact, when C++ users pass an array element to a function, it automatically converts it to a pointer (to the first array element).
Example of passing an array using pointer operators.
#include <iostream>
using namespace std;
void updateArray(int* ptr, int size) {
for (int p = 0; p < size; p++) {
ptr[p] /= 5; // here it divides 5 by the numeric value of each element in the array
}
}
int main() {
int array[6] = {20, 30, 40, 80, 100, 170};
// Here, pass the array using a special pointer operator
updateArray(array, 6);
// Here, print the modified array with the new element value
for (int p = 0; p < 6; p++) {
cout << array[p] << ” “; // Result – 4 6 8 16 20 34
}
cout << endl;
return 0;
}
Explanation of passing an array using pointer operators.
- In this example, the array data type is passed as a pointer (int* ptr) to the updateArray user-defined function. Inside the updateArray function, ptr points to the first element of the array, and the array element data value can be modified by dereferencing it with a pointer operator.
Passing an array element with a constant data type size in C++.
If the C++ user already knows the size of the array storage element in a program, they can manually define the array size in the existing function signature. This helps the C++ user by allowing the compiler to optimize the existing function array code in a C++ program.
Example of passing a fixed-size array.
#include <iostream>
using namespace std;
// Here we define a function that accepts a 2D array with a fixed 4×4 array element size.
void pview2dArray(int array[4][4]) {
for (int p = 0; p < 4; p++) {
for (int q = 0; q < 4; q++) {
cout << array[p][q] << ” “; // Here it prints each 2D array element of the array.
}
cout << endl;
}
}
int main() {
int array[4][4] = {{100, 199, 133, 144}, {999, 111, 333, 444} , {555, 666, 777, 1000}, {888, 488, 336, 789}};
// here it Pass the 2D array to the user-defined peview2dArray function
peview2dArray(array);
return 0;
}
Explanation of passing a fixed-size array.
- In this example, a 2D array is created in the peview2dArray user-defined function. Here, the size of peview2dArray is fixed, defined as 4 rows and 4 columns. This allows peview2dArray to access the elements directly without passing the array size as an argument to the function.
