How arrays are passed to functions in C/C++
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.
#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; } |
chevron_right
filter_none
Output:
Array size inside main() is 8 Array size inside fun() is 1
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.

#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; } |
chevron_right
filter_none
Output:
1 2 3 4 5 6 7 8
Exercise:
Predict the output of below C programs:
-
#include <stdio.h>voidfun(intarr[], unsignedintn){inti;for(i=0; i<n; i++)printf("%d ", arr[i]);}// Driver programintmain(){intarr[] = {1, 2, 3, 4, 5, 6, 7, 8};unsignedintn =sizeof(arr)/sizeof(arr[0]);fun(arr, n);return0;}chevron_rightfilter_none -
#include <stdio.h>voidfun(int*arr){inti;unsignedintn =sizeof(arr)/sizeof(arr[0]);for(i=0; i<n; i++)printf("%d ", arr[i]);}// Driver programintmain(){intarr[] = {1, 2, 3, 4, 5, 6, 7, 8};fun(arr);return0;}chevron_rightfilter_none -
#include <stdio.h>#include <string.h>voidfun(char*arr){inti;unsignedintn =strlen(arr);printf("n = %d\n", n);for(i=0; i<n; i++)printf("%c ", arr[i]);}// Driver programintmain(){chararr[] ="geeksquiz";fun(arr);return0;}chevron_rightfilter_none -
#include <stdio.h>#include <string.h>voidfun(char*arr){inti;unsignedintn =strlen(arr);printf("n = %d\n", n);for(i=0; i<n; i++)printf("%c ", arr[i]);}// Driver programintmain(){chararr[] = {'g','e','e','k','s','q','u','i','z'};fun(arr);return0;}chevron_rightfilter_none - Functions in C/C++
- Searching in a map using std::map functions in C++
- Thread functions in C/C++
- Nested functions in C
- C | Functions | Question 10
- C | Functions | Question 9
- C | Functions | Question 8
- C | Functions | Question 7
- C | Functions | Question 6
- C | Functions | Question 5
- C | Functions | Question 4
- C | Functions | Question 3
- C | Functions | Question 2
- C | Functions | Question 11
- Inline Functions in C++
Note that the character array in 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 above
Recommended Posts:
Improved By : RishabhPrabhu



