Passing Array to Function in C++
To use arrays efficiently we should know how to pass arrays to function. We can pass arrays to functions as an argument same as we pass variables to functions but we know that the array name is treated as a pointer using this concept we can pass the array to functions as an argument and then access all elements of that array using pointer.
So ultimately, arrays are always passed as pointers to the function. Let’s see 3 ways to pass an array to a function that are majorly used.
1. Passing Array as a Pointer
In this method, we simply pass the array name in function call which means we pass the address to the first element of the array. In this method, we can modify the array elements within the function.
Syntax
return_type function_name ( data_type *array_name ) {
// set of statements
}
2. Passing Array as an Unsized Array
In this method, the function accepts the array using a simple array declaration with no size as an argument.
Syntax
return_type function_name ( data_type array_name[] ) {
// set of statements
}
3. Passing Array as a Sized Array
In this method, the function accepts the array using a simple array declaration with size as an argument. We use this method by sizing an array just to indicate the size of an array.
Syntax
return_type function_name(data_type array_name[size_of_array]){
// set of statements
}
Note: Array will be treated as a pointer in the passed function no matter what method we use. As the array are passed as pointers, they will loose the information about its size leading to a phenomenon named as Array Decay.
Example: Illustrating Different Ways to Pass Arrays to a Function
#include <iostream>
using namespace std;
// passing array as a sized array argument
void printArraySized(int arr[3], int n)
{
cout << "Array as Sized Array Argument: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
// passing array as an unsized array argument
void printArrayUnsized(int arr[], int n)
{
cout << "Array as Unsized Array Argument: ";
for (int i = 0; i < n; i++) {
cout << *(arr + i) << " ";
}
cout << endl;
}
// Passing array as a pointer argument
void printArrayPointer(int* ptr, int n)
{
// Print array elements using pointer ptr
// that store the address of array passed
cout << "Array as Pointer Argument: ";
for (int i = 0; i < n; i++) {
cout << ptr[i] << " ";
}
}
// driver code
int main()
{
int arr[] = { 10, 20, 30 };
// Call function printArray and pass
// array and its size to it.
printArraySized(arr, 3);
printArrayUnsized(arr, 3);
printArrayPointer(arr, 3);
return 0;
}
Output
Array as Sized Array Argument: 10 20 30 Array as Unsized Array Argument: 10 20 30 Array as Pointer Argument: 10 20 30
C++ Arrays
In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location.
For example, if we have to store the marks of 4 or 5 students then we can easily store them by creating 5 different variables but what if we want to store marks of 100 students or say 500 students then it becomes very challenging to create that numbers of variable and manage them. Now, arrays come into the picture that can do it easily by just creating an array of the required size.