
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pass an Array by Reference in C++
Passing an array by reference is a method of passing arrays to functions in C++ that allows the function to access and modify the original array without creating a copy. In this article, we will learn how to pass arrays by reference, the benefits of this approach, and provide examples.
Passing an Array by Reference
In the pass by reference approach, the address location of the first element of the array is passed to user defined functions. So, technically, we are passing the same array to that function without creating a copy. The changes made to the array inside the function will also reflect outside the function. Let's look at an example to understand how to implement this in C++.
// Main function declares an array int main() { int arr[] = {1, 2, 3, 4, 5}; } // Function to modify the array void modifyArray(int (&arr)[5]) { arr[0] *= 2; // Modify first element } // Main function try to print the modified array cout << arr[0] << " " << arr[1]; // Output: 2 2
In C++, by default arrays are passed by reference. Means, when you pass an array to a function, it actually passes a pointer to the first element. There are two case to consider when passing an array by reference, when array's type and size is fixed and when array is of unknown type and size.
Passing Fixed Size Array by Reference
If the receiving function knows the size of the array at compile time, you can directly pass the array by reference using just the array name in main function. At parameter of receiving function, you have to specify the size of the array in square brackets. The code below shows syntax for passing a fixed-size array by reference:
// Function declaration void modifyArray(int (&arr)[5]); //Call the function int arr[5] = {10, 20, 30, 40, 50}; modifyArray(arr);
Now, let's see a working example for this approach.
Example
In the example below, we shows how to pass a fixed-size array by reference to a function that modifies its elements:
#include <iostream> using namespace std; // Function to modify array elements void modifyArray(int (&arr)[5]) { for (int i = 0; i < 5; ++i) { arr[i] *= 2; } } int main() { int arr[5] = {10, 20, 30, 40, 50}; modifyArray(arr); cout << "Modified array: "; for (int i = 0; i < 5; ++i) { cout << arr[i] << " "; } return 0; }
The output of the above code will be:
Modified array: 20 40 60 80 100
Passing Array by Reference Using Template
Sometimes, you may not know the size or type of the array at compile time. In such cases, you can use templates to create a function that can accept arrays of any size and type by reference. A template function allows you to define a function that can work with any data type and array size. The syntax is shown below:
template <typename T, size_t N> void modifyArray(T (&arr)[N]) {} // Call the function T arr[N] = {1, 2, 3, 4, 5}; modifyArray(arr);
Example
The example shows how to define a template function that accepts an array of any size by reference and prints its contents:
#include <iostream> using namespace std; // Template function to print array template<typename T, size_t N> void printArray(T (&arr)[N]) { for (size_t i = 0; i < N; ++i) { cout << arr[i] << " "; } cout << endl; } int main() { int arr1[4] = {1, 2, 3, 4}; double arr2[3] = {1.1, 2.2, 3.3}; cout << "Integer array: "; printArray(arr1); cout << "Double array: "; printArray(arr2); return 0; }
The output of the above code will be:
Integer array: 1 2 3 4 Double array: 1.1 2.2 3.3