Skip to content
Related Articles

Related Articles

How arrays are passed to functions in C/C++
  • Difficulty Level : Medium
  • Last Updated : 21 Oct, 2020

In C, when we pass an array to a function say fun(), it is always treated as a pointer by fun(). Below example demonstrates the same.

C




#include <stdio.h>
 
// Note that arr[] for fun is just a pointer even if square
// brackets are used
void 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 program
int 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 2

Therefore in C, we must pass size of array as a parameter. Size may not be needed only in case of ‘\0’ terminated character arrays, size can be determined by checking end of string character.
Following is a simple example to show how arrays are typically passed in C.
 


C




#include <stdio.h>
 
void fun(int *arr, unsigned int n)
{
   int i;
   for (i=0; i<n; i++)
     printf("%d  ", arr[i]);
}
 
// Driver program
int 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 below C programs:
 

1.

C




#include <stdio.h>
void fun(int arr[], unsigned int n)
{
int i;
for (i=0; i<n; i++)
    printf("%d ", arr[i]);
}
 
// Driver program
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
unsigned int n = sizeof(arr)/sizeof(arr[0]);
fun(arr, n);
return 0;
}


2.

C




#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 program
int main()
{
   int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
   fun(arr);
   return 0;
}


3.

C




#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 program
int main()
{
   char arr[] = "geeksquiz";
   fun(arr);
   return 0;
}


4.

C




#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 program
int main()
{
   char arr[] = {'g', 'e', 'e', 'k', 's', 'q', 'u', 'i', 'z'};
   fun(arr);
   return 0;
}


Note that the character array in the above program is not ‘\0’ terminated. See this for details.

Related Articles:

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed here.

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :