You must be aware of the methods of function calling for better understanding of this topic. Array function call is the best example of the call by reference as we have to pass array reference only to access the complete array through the pointer.
Function call is always based on your requirement. to work on array you may have one of the following requirments-
— You may need to write a function which takes a single element of an array as function Input.
Function Calling
1 |
function_name(array_name[index]); |
Function Definition
1 |
return_type function_name(dataType arr_value){;} |
— You may need to write a function which takes a whole one-dimensional array as function Input.
Function Calling
1 |
function_name(&array_name[0]); |
1 |
function name(array_name); |
Function Definition
1 |
return_type function_name(dataType *arr_ptr){;} |
1 |
return_type function_name(dataType arr_ptr[]){;} |
1 |
return_type function_name(dataType arr_ptr[array_size]){;} |
— You may need to write a function which takes a multi-dimensional array as function Input.
Function Calling
1 |
function_name(&array_name); |
Function Definition
1 |
return_type function_name(dataType (*arr_ptr)[]){;} |
Note:
— array_name ,&array_name[0] ,&array_name all represents the base address of an one dimensional array.
— (array_name+1)=(&array_name[0]+1) but not equal to (&array_name+1).
1 |
array_name+1= address of 1st indexed element of one dimensional array. |
1 |
&array_name[0]+1= address of 1st indexed element of one dimensional array. |
1 |
&array_name+1 = Address of the 1st indexed array of multidimensional array. |
Program to display an initialized array through an user-defined function display().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> void display(unsigned int *,unsigned int );/*protoType*/ int main() { unsigned int arr[10]={10,20,30,40,50,60}; display(arr,6); /*calling*/ return 0; } void display(unsigned int *arr_ptr,unsigned int size) /*definition*/ { unsigned int j; for(j=0;j<size;j++) { printf("\narr[%d]= %u",j,arr_ptr[j]); } } |
Output:
1 2 3 4 5 6 |
arr[0]= 10 arr[1]= 20 arr[2]= 30 arr[3]= 40 arr[4]= 50 arr[5]= 60 |