
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
Passing Arrays to Function in C++
In this article, we will learn different ways to pass arrays to functions in C++ and how to work with them effectively.
Passing Arrays to Functions
Here are list of approaches to pass arrays to functions in C++ that will be discussed in this article along with examples:
- As a Sized Array
- As an Unsized Array
- As a Pointer (Pass by Pointer)
- As a Reference (Pass by Reference)
As a Sized Array
This method passes an array to a function by specifying its exact size in the parameter. The syntax for this method is as follows:
void display(int arr[5]);
In this syntax, int arr[5] indicates that the array has exactly 5 elements. Let's see a working example:
Example
In the code below, we will define a function display() that accepts a sized array and prints its elements.
#include <iostream> using namespace std; void display(int arr[5]) { for (int i = 0; i < 5; i++) { cout << arr[i] << " "; } cout << endl; } int main() { int myArray[5] = {10, 20, 30, 40, 50}; display(myArray); return 0; }
The output of the above code will be:
10 20 30 40 50
As an Unsized Array
In this method, the array size is specified as a separate function parameter along with the array itself. The syntax for this method is as follows:
void display(int arr[], int size);
In this syntax, int arr[] is treated as a pointer, and the size is passed separately. Let's see a working example.
Example
In the code below, we will pass an unsized array and its size to print the elements using a loop.
#include <iostream> using namespace std; void display(int arr[], int size) { for (int i = 0; i < size; i++) { cout << arr[i] << " "; } cout << endl; } int main() { int myArray[4] = {1, 3, 5, 7}; display(myArray, 4); return 0; }
The output of the above code will be:
1 3 5 7
As a Pointer
In this method, we will pass the array as a pointer to the function. The pointer will point to the first element of the array and rest of elements can be reached using pointer arithmetic. The syntax for this method is as follows:
void display(int* arr, int size);
In this syntax, int* arr is a pointer to the first element of the array. Let's see a working example.
Example
In the code below, we will pass a dynamic array to a function using a pointer and print its values.
#include <iostream> using namespace std; void display(int* arr, int size) { for (int i = 0; i < size; i++) { cout << arr[i] << " "; } cout << endl; } int main() { int* myArray = new int[3]{4, 8, 12}; display(myArray, 3); delete[] myArray; return 0; }
The output of the above code will be:
4 8 12
As a Reference
In this method, the array is passed by reference, which prevents the array decay into a pointer. The size of array must be known at compile time. Hence dynamic array cannot be passed using this method. The syntax for this method is as follows:
void display(int (&arr)[3]);
In this syntax, int (&arr)[3] means arr is a reference to an array of 3 integers. Let's see a working example.
Example
In the code below, we will pass a fixed-size array by reference and print its contents inside the function.
#include <iostream> using namespace std; void display(int (&arr)[3]) { for (int i = 0; i < 3; i++) { cout << arr[i] << " "; } cout << endl; } int main() { int myArray[3] = {7, 14, 21}; display(myArray); return 0; }
The output of the above code will be:
7 14 21