How to Pass Arrays to Functions in C++: A Practical Guide
How to Pass Arrays to Functions in C++
This guide explains how to pass single‑ and multi‑dimensional arrays to functions in C++ using real code examples.
In C++, arrays can be supplied as function arguments, and you can even return them indirectly via pointers. Understanding the mechanics behind array decay to pointers and the impact on memory and performance is essential for writing efficient code.
Syntax for Passing Arrays as Function Parameters
The declaration for a function that accepts an array looks like this:
returnType functionName(dataType arrayName[arraySize]) {
// function body
}
Example:
int total(int marks[5]) {
// implementation
}
Here, marks is an int array of five elements. When the function is called, only the array’s name (a pointer to its first element) is passed.
Example 1: Passing a One‑Dimensional Array to a Function
#include <iostream>
using namespace std;
// Declare function to display marks
void display(int m[5]) {
cout << "Displaying marks:" << endl;
for (int i = 0; i < 5; ++i) {
cout << "Student " << i + 1 << ": " << m[i] << endl;
}
}
int main() {
int marks[5] = {88, 76, 90, 61, 69};
display(marks); // Pass array by name
return 0;
}
Output
Displaying marks: Student 1: 88 Student 2: 76 Student 3: 90 Student 4: 61 Student 5: 69
Key points:
- Calling
display(marks);passes the address ofmarks[0]. - Inside the function,
int m[5]is treated asint* m;, pointing to the same memory. - Modifying
m[i]changes the originalmarksarray, illustrating pass‑by‑reference semantics.
Passing a Multi‑Dimensional Array to a Function
For 2‑D arrays, you must specify the number of columns (the size of the second dimension) so the compiler can calculate element offsets.
Example 2: Passing a Two‑Dimensional Array to a Function
#include <iostream>
using namespace std;
void display(int n[][2]) {
cout << "Displaying values:" << endl;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 2; ++j) {
cout << "num[" << i <<
"][" << j <<
"]: " << n[i][j] << endl;
}
}
}
int main() {
int num[3][2] = {{3, 4}, {9, 5}, {7, 1}};
display(num);
return 0;
}
Output
Displaying values: num[0][0]: 3 num[0][1]: 4 num[1][0]: 9 num[1][1]: 5 num[2][0]: 7 num[2][1]: 1
Notes:
- The row count (3) is optional because the compiler can infer it from the array definition.
- Specifying the column size (
[][2]) is mandatory for correct pointer arithmetic.
Higher‑dimensional arrays follow the same rule: the size of all dimensions except the first must be specified.
Returning an Array from a Function
Functions cannot return an entire array directly. Instead, they return a pointer to the first element, which may refer to a statically allocated array, a dynamic array, or a temporary object. Care must be taken to avoid returning pointers to local (stack) arrays, which would result in dangling references.
Example (static array):
int* getArray() {
static int arr[5] = {1, 2, 3, 4, 5};
return arr; // pointer to first element
}
For dynamic allocation, consider returning a std::vector<T> for safety and automatic memory management.
Further reading on safe array handling in C++ is recommended for production code.
C Language
- Mastering C++ Functions: From Basics to Advanced Usage
- C++ Function Overloading: A Practical Guide
- C++ Default Function Arguments: How They Work & Best Practices
- Mastering C++ Arrays: Declaration, Initialization, and Practical Examples
- Mastering Multidimensional Arrays in C++: Declaring, Initializing, and Looping
- C++ Function Overriding Explained – Practical Examples & Best Practices
- C++ Friend Functions and Friend Classes: Mastering Access Control
- Mastering C++ Virtual Functions: Concepts, Examples, and the Override Keyword
- Understanding Polymorphism in C++: A Practical Guide
- Master C++ Web Programming with CGI