If you want to pass an array to a function, you can use either call by value or call by reference method. In “call by value” method, the argument to the function should be an initialized array. Here, an array of fixed size equal to the size of the array to be passed. In “call by reference” method, the function argument is a pointer to the array.
Passing an Array with Call by Value Method
In the following code, the main() function has an array of integers. A user-defined function average() is called by passing the array to it. The average() function receives the array and adds its elements using a for loop. It returns a float value representing the average of numbers in the array.
Example 1
Take a look at the example −
#include <stdio.h>floataverage(int arr[5]);intmain(){int arr[]={10,34,21,78,5};float avg =average(arr);printf("Average: %f", avg);}floataverage(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
When you run this code, it will produce the following output −
arr[0]: 10
arr[1]: 34
arr[2]: 21
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 the main() function is obtained by divising the size of the array with the size of int data type.
Example 2
Take a look at the example −
#include <stdio.h>floataverage(int arr[],int length);intmain(){int arr[]={10,34,21,78,5};int length =sizeof(arr)/sizeof(int);float avg =average(arr, length);printf("Average: %f", avg);}floataverage(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
When you run this code, it will produce the following output −
arr[0]: 10
arr[1]: 34
arr[2]: 21
arr[3]: 78
arr[4]: 5
Average: 29.600000
Passing an Array with Call by Reference Method
To use this approach, we should understand that the 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.
In the following example −
int a[5]={1,2,3,4,5};
The size of the array is 20 bytes (4 bytes for each int).
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 1
Take a look at the example −
#include <stdio.h>intmain(){int a[]={1,2,3,4,5};int*x = a, i;for(i =0; i <5; i++){printf("%d\n",*x);
x++;}return0;}
Output
When you run this code, it will produce the following 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 the main() function.
Example 2
Take a look at the example −
#include <stdio.h>intmax(int*arr,int length);intmain(){int arr[]={10,34,21,78,5};int length =sizeof(arr)/sizeof(int);int maxnum =max(arr, length);printf("max: %d", maxnum);}intmax(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
When you run this code, it will produce the following 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 −
intmax(int*arr,int length){int max =*arr;int i;for(i =0; i < length; i++){printf("arr[%d]: %d\n", i, arr[i]);if(arr[i]> max)
max = arr[i];}return max;}
Pass a Two-dimensional Array to a Function
You can also pass the pointer of a two-dimensional array to a function. Inside the function, the two-dimensional array is traversed with a nested for loop construct.
Example
Take a look at the following example −
#include <stdio.h>inttwoDarr(int*arr);intmain(){int arr[][3]={10,34,21,78,5,25};twoDarr(*arr);}inttwoDarr(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
When you run this code, it will produce the following output −
10 34 21
5 25 16
Function to Compare String Lengths
In the following program, two strings are passed to the compare() function. In C, a string is an array of char data type. We use the strlen() function to find the length of a string which is the number of characters in it.
Example
Take a look at the following example −
#include <stdio.h>#include <string.h>intcompare(char*,char*);intmain(){char a[]="BAT";char b[]="BALL";int ret =compare(a, b);return0;}intcompare(char*x,char*y){int val;if(strlen(x)>strlen(y)){printf("The length of string 'a' is greater than or equal to the length of string 'b'");}else{printf("The length of string 'a' is less than the length of string 'b'");}}
Output
When you run this code, it will produce the following output −
The length of string 'a' is less than the length of string 'b'
Leave a Reply