Passing Arrays As Function Arguments in C
Passing Arrays As Function Arguments in C
Example
#include <stdio.h>
float average(int arr[5]);
int main(){
int arr[] = {10, 34, 21, 78, 5};
float avg = average(arr);
printf("average: %f", avg);
}
float average(int arr[5]){
int sum=0;
int i;
for (i=0; i<5; i++){
printf("arr[%d]: %d\n", i, arr[i]);
sum+=arr[i];
}
return (float)sum/5;
}
Output
arr[0]: 10
arr[1]: 34
arr[2]: 21
https://www.tutorialspoint.com/cprogramming/c_passing_arrays_to_functions.htm 1/6
6/16/24, 12:41 PM Passing Arrays as Function Arguments in C
arr[3]: 78
arr[4]: 5
average: 29.600000
In the following variation, the average() function is defined with two arguments, an
uninitialized array without any size specified. The length of the array declared in
main() function is obtained by divising the size of the array with the size of int data
type.
Example
#include <stdio.h>
float average(int arr[], int length);
int main(){
int arr[] = {10, 34, 21, 78, 5};
int length = sizeof(arr)/sizeof(int);
float avg = average(arr, length);
printf("average: %f", avg);
}
float average(int arr[], int length){
int sum=0;
int i;
for (i=0; i<length; i++){
printf("arr[%d]: %d\n", i, arr[i]);
sum+=arr[i];
}
return (float)sum/length;
}
Output
arr[0]: 10
arr[1]: 34
arr[2]: 21
arr[3]: 78
arr[4]: 5
average: 29.600000
https://www.tutorialspoint.com/cprogramming/c_passing_arrays_to_functions.htm 2/6
6/16/24, 12:41 PM Passing Arrays as Function Arguments in C
To use this approach, we should understand that elements in an array are of similar
data type, stored in continuous memory locations, and the array size depends on the
data type. Also, the address of the 0th element is the pointer to the array.
Int *x = a;
Here x is the pointer to the array. It points to the 0th element. If the pointer is
incremented by 1, it points to the next element.
Example
#include <stdio.h>
int main() {
int a[] = {1,2,3,4,5};
int *x = a, i;
for (i=0; i<5; i++){
printf("%d\n", *x);
x++;
}
return 0;
}
Output
1
2
3
4
5
Let us use this characteristics for passing the array by reference. In the main()
function, we declare an array and pass its address to the max() function. The max()
function traverses the array using the pointer and returns the largest number in the
array, back to main() function.
https://www.tutorialspoint.com/cprogramming/c_passing_arrays_to_functions.htm 3/6
6/16/24, 12:41 PM Passing Arrays as Function Arguments in C
Example
#include <stdio.h>
int max(int *arr, int length);
int main(){
int arr[] = {10, 34, 21, 78, 5};
int length = sizeof(arr)/sizeof(int);
int maxnum = max(arr, length);
printf("max: %d", maxnum);
}
int max(int *arr, int length){
int max=*arr;
int i;
for (i=0; i<length; i++){
printf("arr[%d]: %d\n", i, (*arr));
if ((*arr)>max)
max = (*arr);
arr++;
}
return max;
}
Output
arr[0]: 10
arr[1]: 34
arr[2]: 21
arr[3]: 78
arr[4]: 5
max: 78
The max() function receives the address of the array from main() in the pointer arr.
Each time, when it is incremented, it points to the next element in the original array.
The max() function can also access the array elements as a normal subscripted array
as in the following definition −
https://www.tutorialspoint.com/cprogramming/c_passing_arrays_to_functions.htm 4/6
6/16/24, 12:41 PM Passing Arrays as Function Arguments in C
Example
#include <stdio.h>
int twoDarr(int *arr);
int main(){
int arr[][3]= {10, 34, 21, 78, 5, 25};
twoDarr(*arr);
}
int twoDarr(int *arr){
int max=*arr;
int i, j;
for (i=0; i<2; i++){
for (j=0; j<3; j++){
printf("%d\t", arr[i]);
arr++;
}
printf("\n");
}
}
Output
10 34 21
5 25 16
https://www.tutorialspoint.com/cprogramming/c_passing_arrays_to_functions.htm 5/6
6/16/24, 12:41 PM Passing Arrays as Function Arguments in C
Example
#include <stdio.h>
#include <string.h>
int compare( char *, char *);
int main() {
char a[] = "BAT";
char b[] = "BALL";
int ret = compare(a, b);
return 0;
}
int compare (char *x, char *y){
int val;
if (strlen(x)>strlen(y)){
printf("length of string a is greater than or equal to length of string b");
}
else{
printf("length of string a is less than length of string b");
}
}
Output
https://www.tutorialspoint.com/cprogramming/c_passing_arrays_to_functions.htm 6/6