How Arrays are Passed to Functions in C/C++? - GeeksforGeeks

09 Jan, 2022
A whole array cannot be passed as an argument to a function in C++. You can, however, pass a pointer to an array without an index by specifying the array’s name.
In C, when we pass an array to a function say fun(), it is always treated as a pointer by fun(). The below example demonstrates the same. 
C++
C
// CPP Program to demonstrate passing// an array to a function is always treated// as a pointer#include <iostream>using namespace std; // Note that arr[] for fun is// just a pointer even if square// brackets are usedvoid fun(int arr[]) // SAME AS void fun(int *arr){    unsigned int n = sizeof(arr) / sizeof(arr[0]);    cout << "\nArray size inside fun() is " << n;} // Driver Codeint main(){    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };    unsigned int n = sizeof(arr) / sizeof(arr[0]);    cout << "Array size inside main() is " << n;    fun(arr);    return 0;}
#include <stdio.h>#include <stdlib.h>// Note that arr[] for fun is just a pointer even if square// brackets are usedvoid fun(int arr[])  // SAME AS void fun(int *arr){   unsigned int n = sizeof(arr)/sizeof(arr[0]);   printf("\nArray size inside fun() is %d", n);} // Driver programint main(){   int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};   unsigned int n = sizeof(arr)/sizeof(arr[0]);   printf("Array size inside main() is %d", n);   fun(arr);   return 0;}
Output
Array size inside main() is 8
Array size inside fun() is 1
Therefore in C, we must pass the size of the array as a parameter. Size may not be needed only in the case of ‘\0’ terminated character arrays, size can be determined by checking the end of string character.
Following is a simple example to show how arrays are typically passed in C: 
C++
C
#include <iostream>using namespace std; void fun(int *arr, unsigned int n){   int i;   for (i = 0; i < n; i++)     cout <<" "<< arr[i];} // Driver programint main(){   int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};   unsigned int n = sizeof(arr)/sizeof(arr[0]);   fun(arr, n);   return 0;}
#include <stdio.h> void fun(int *arr, unsigned int n){   int i;   for (i=0; i<n; i++)     printf("%d  ", arr[i]);} // Driver programint main(){   int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};   unsigned int n = sizeof(arr)/sizeof(arr[0]);   fun(arr, n);   return 0;}
Output
1  2  3  4  5  6  7  8
Exercise: Predict the output of the below C programs:
Program 1:
C++
C
// Program 1#include <iostream>using namespace std;void fun(int arr[], unsigned int n){    int i;    for (i = 0; i < n; i++)        cout << arr[i] << " ";} // Driver programint main(){    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };    unsigned int n = sizeof(arr) / sizeof(arr[0]);    fun(arr, n);    return 0;}
#include <stdio.h>void fun(int arr[], unsigned int n){int i;for (i=0; i<n; i++)    printf("%d ", arr[i]);} // Driver programint main(){int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};unsigned int n = sizeof(arr)/sizeof(arr[0]);fun(arr, n);return 0;}
Output
1 2 3 4 5 6 7 8
 Program 2:
C++
C
// Program 2#include <iostream>using namespace std;void fun(int* arr){    int i;    unsigned int n = sizeof(arr) / sizeof(arr[0]);    for (i = 0; i < n; i++)        cout << " " << arr[i];} // Driver programint main(){    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };    fun(arr);    return 0;}
#include <stdio.h>void fun(int *arr){   int i;   unsigned int n = sizeof(arr)/sizeof(arr[0]);   for (i=0; i<n; i++)     printf("%d  ", arr[i]);} // Driver programint main(){   int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};   fun(arr);   return 0;}
Output
 1 2
Program 3:
C++
C
// Program 3#include <iostream>#include <string.h>using namespace std;void fun(char* arr){    int i;    unsigned int n = strlen(arr);    cout << "n = " << n << endl;    for (i = 0; i < n; i++)        cout << arr[i] << " ";} // Driver programint main(){    char arr[] = "geeksquiz";    fun(arr);    return 0;}
#include <stdio.h>#include <string.h> void fun(char *arr){   int i;   unsigned int n = strlen(arr);   printf("n = %d\n", n);   for (i=0; i<n; i++)     printf("%c  ", arr[i]);} // Driver programint main(){   char arr[] = "geeksquiz";   fun(arr);   return 0;}
Output
n = 9
g e e k s q u i z 
Program 4: 
C++
C
// Program 4#include <bits/stdc++.h>#include <iostream>using namespace std; void fun(char* arr){    int i;    unsigned int n = strlen(arr);    cout << "n = " << n << "\n";    for (i = 0; i < n; i++)        cout << " " << arr[i];} // Driver programint main(){    char arr[]        = { 'g', 'e', 'e', 'k', 's', 'q', 'u', 'i', 'z' };    fun(arr);    return 0;}
#include <stdio.h>#include <string.h> void fun(char *arr){   int i;   unsigned int n = strlen(arr);   printf("n = %d\n", n);   for (i=0; i<n; i++)     printf("%c  ", arr[i]);} // Driver programint main(){   char arr[] = {'g', 'e', 'e', 'k', 's', 'q', 'u', 'i', 'z'};   fun(arr);   return 0;}
Output
n = 11
g e e k s q u i z
NOTE: The character array in the above program is not ‘\0’ terminated. (See this for details)
Now These were some of the common approaches that we use but do you know that there is a better way to do the same. For this, we first need to look at the drawbacks of all the above-suggested methods:
Drawbacks:
A major drawback of the above method is compiler has no idea about what you are passing. What I mean here is for compiler we are just passing an int* and we know that this is pointing to the array but the compiler doesn’t know this. 
To verify my statement you can call for-each loop on your array. You will surely get an error saying no callable begin, end function found.This is because the passing array is like actually passing an integer pointer and it itself has no information about the underlying array hence no iterator is provided.
This method retains all information about the underlying array. This method is majorly based on reference to an array but using this with templates optimizes our method. Template dependency actually calculates the length of the array automatically at the time of function call so that it can be used to create a reference because a reference to an array must know the size of the array.
Here Template is used for template argument deduction.
C++
// CPP Program to demonstrate template approach#include <iostream>using namespace std; template <size_t N> void print(int (&a)[N]){    for (int e : a) {        cout << e << endl;    }} // Driver Codeint main(){    int a[]{ 1, 2, 3, 4, 5 };    print(a);}
1
2
3
4
5
Here you can see why we need template argument deduction. For a base to create a reference to an array so that we can take an array as a parameter. 
Related Articles:
Do not use sizeof for array parameters.
Difference between pointer and array in C?
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed here.
RishabhPrabhu
prasadguru15698
mohdatishanx
uppala0863
shivanisinghss2110
anshikajain26
CBSE - Class 11
cpp-array
CPP-Functions
cpp-pointer
school-programming
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
TCP Server-Client implementation in C
Exception Handling in C++
Multithreading in C
'this' pointer in C++
UDP Server-Client implementation in C
Vector in C++ STL
Inheritance in C++
Initialize a vector in C++ (6 different ways)
Map in C++ Standard Template Library (STL)
C++ Classes and Objects