C Programming Tutorial
Computer Tutorial

Passing an Entire Array to a Function



Passing an Entire Array to a Function

In C, we cannot pass an entire array as an argument to a function. When we passing the entire array into a function then we just passing the address of the zeroth element of the array to a function and obtained the other array element by increment in the value of pointer.
The address of the zero'th element (many a times called the base address) can also be passed by just passing the name of the array.
Thus, the following two statement are same :
& num[ 0 ] ;
num ;
Both point out the same element. Both indicate the zero'th element of the array ‘name’.

A Simple C Program to pass an array into a function.

# include < stdio.h >
int   main( )
{

int   marks[] = { 45, 64, 95, 40, 85, 79, 93 }  ;
display(   & marks[0], 7 ) ;
return ( 0 );

}
display( int *j, int n ) ;
{
for ( i = 1; i < n ; i ++  )
{

printf(" \n element = %d ",   *j ) ;
j++ ;

}

}

The following all array pointer notation are same. All point out the same memory location.

num[i] ;
*( num + i ) ;
*( i + num ) ;
i[num] ;