← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 6 of 15

C++ Arrays & Functions

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.