C++ Arrays & Functions
In this page:
Passing Arrays to Functions
Passing an array to a function uses the array's name without brackets as the argument, while the receiving parameter is written with empty brackets, like void process(int arr[]). Under the hood, what's actually passed is the array's starting memory address, not a full copy of its contents.
Example: Passing Arrays to Functions
#include <iostream>
void printArr(int arr[], int size) {
for (int i = 0; i < size; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
int main() {
int nums[] = {1, 2, 3};
printArr(nums, 3);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Passing Array Size
Because an array parameter is really just a pointer to its first element, the function has no way to know how many elements the original array actually contains — you have to pass the size explicitly as a separate integer parameter, like void process(int arr[], int size), or the function risks reading past the array's real bounds.
Example: Passing Array Size
#include <iostream>
void printArr(int arr[], int size) {
for (int i = 0; i < size; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
int main() {
int nums[] = {4, 5, 6, 7};
printArr(nums, 4); // size must be passed separately
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Modifying Arrays in Functions
Since arrays are effectively passed by reference (via their memory address), any modification a function makes to the array's elements directly changes the original array back in the caller — unlike ordinary variables, there's no automatic protective copy being made.
Example: Modifying Arrays in Functions
#include <iostream>
void doubleAll(int arr[], int size) {
for (int i = 0; i < size; i++) {
arr[i] *= 2;
}
}
int main() {
int nums[] = {1, 2, 3};
doubleAll(nums, 3);
std::cout << nums[0] << " " << nums[1] << " " << nums[2] << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Passing Multi-dimensional Arrays
When passing a 2D array to a function, the first dimension's size can be omitted from the parameter, like void process(int grid[][4]), but the second (column) dimension must always be specified exactly, since the compiler needs it to calculate the memory offset for each row.
Example: Passing Multi-dimensional Arrays
#include <iostream>
void process(int grid[][4], int rows) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 4; j++) {
std::cout << grid[i][j] << " ";
}
}
std::cout << std::endl;
}
int main() {
int grid[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
process(grid, 2);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Const Array Parameters
Adding const to an array parameter, like void print(const int arr[], int size), signals that the function will only read the array's contents and promises not to modify them — the compiler will then flag any accidental write inside the function as an error, catching a class of bugs before the program even runs.
Example: Const Array Parameters
#include <iostream>
void print(const int arr[], int size) {
for (int i = 0; i < size; i++) {
std::cout << arr[i] << " ";
// arr[i] = 0; // compile error: cannot modify a const array parameter
}
std::cout << std::endl;
}
int main() {
int nums[] = {1, 2, 3};
print(nums, 3);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- C++ Arrays Introduction
- C++ Looping Through Arrays
- C++ Omitting Array Size
- C++ Array Size
- C++ Multi-dimensional Arrays
- C++ Arrays & Functions
- C++ Strings (C-style)
- C++ std::string
- C++ String Concatenation
- C++ Converting Strings and Numbers
- C++ String Length
- C++ Accessing String Characters
- C++ using namespace std
- C++ String Methods
- C++ Array of Strings