In C it is impossible to pass an entire array as an argument to a function -- instead the address of the array is passed as a parameter to the function. (In time we will regard this as a pointer).
The name of an array without any index is the address of the first element of the array and hence of the whole array as it is stored contiguously. However we need to know the size of the array in the function - either by passing an extra parameter or by using the sizeof operator..
For Example :-
void main() { int array[20] ; func1( array ) ;/* passes pointer to array to func1 */ }
Since we are passing the address of the array the function will be able to manipulate the actual data of the array in main(). This is call by reference as we are not making a copy of the data but are instead passing its address to the function. Thus the called function is manipulating the same data space as the calling function.
In the function receiving the array the formal parameters can be declared in one of three almost equivalent ways as follows :-
• As a sized array : func1 ( int x[10] ) { ... } • As an unsized array : func1 ( int x[ ] ) { ... } • As an actual pointer func1 ( int *x ) { ... }
All three methods are identical because each tells us that in this case the address of an array of integers is to be expected.
NOTE : However that in cases 2 and 3 above where we specify the formal parameter as an unsized array or simply as a pointer we cannot determine the size of the array passed in using the sizeof operator as the compiler does not know what dimensions the array has at this point. Instead sizeof returns the size of the pointer itself, two in the case of near pointers in a 16-bit system but four in 32-bit systems.
For Example :- Program to calculate the average value of an array of doubles.
#include <stdio.h> void read_array( double array[ ], int size ) ; double mean( double array[ ], int size ) ; void main() { double data[ 100 ] ; double average ; read_array( data, 100 ) ; average = mean( data, 100 ) ; } void read_array( double array[ ], int size ) { int i ; for ( i = 0; i<100; i++ ) { printf( “\nEnter data value %d : i + 1 ); scanf( “%lf”, &array[i] ; _flushall() ; } } double mean( double array[ ], int size ) { double total = 0.0 ; int count = size ; while ( count-- ) // size is a local variable which we can // use at will total += array[ count ] ; return ( total / size ) ; }
For Example :- Program to test if a user input string is a palindrome or not.
#include <stdio.h> int palin( char array[ ] ) ; /* Function to determine if array is a palindrome returns 1 if it is a palindrome, 0 otherwise */ void main( ) { char str[100] ; puts( "Enter test string" ) ; gets( str ) ; if ( palin( str ) ) printf( "%s is a palindrome\n", str ) ; else printf( "%s is not a palindrome\n") ; } int palin ( char array[ ] ) { int i = 0, j = 0 ; while ( array[j++] ) ; /* get length of string i.e. increment j while array[j] != "\0" */ j -= 2 ; /* move back two -- gone one beyond "\0" */ for ( i=0 ; i < j ; i++, j-- ) if ( array[ i ] != array[ j ] return 0 ; /* return value 0 if not a palindrome */ return 1 ; /* otherwise it is a palindrome */ }
An alternative way of writing the palin() function might be as follows using string manipulation functions ( must add #include
int palin( char array[ ] ) { char temp[30] ; strcpy( temp, array ) ;/* make a working copy of string */ strrev( temp ) ; /* reverse string */ if ( ! strcmp( temp, array ) ) /* compare strings – if same strcmp returns 0 */ return 1 ; else return 0 ; }
Function calls with multi-dimensional arrays will be the same as with single dimension arrays as we will still only pass the address of the first element of the array.
However to declare the formal parameters to the function we need to specify all but one of the dimensions of the array so that it may be indexed properly in the function.
For Example :-
2D array of doubles :- double x[10][20] ; Call func1 with x a parameter :- func1( x ) ; Declaration in func1 :- func1( double y[ ][20] ) { ... }
The compiler must at least be informed how many columns the matrix has to index it correctly. For example to access element y[5][3] of the array in memory the compiler might do the following
element No = 5 * 20 + 3 = 103.
NOTE : Multi-dimensional arrays are stored row-wise so y[5][3] is the 4th element in the 6th row.
Since we are dealing with an array of doubles this means it must access the memory location 103 X 8 bytes from the beginning of the array.
Thus the compiler needs to know how many elements are in each row of the 2D array above. In general the compiler needs to know all dimensions except the leftmost at the very least.
For Example :- Program to add two 2 x 2 matrices.
#include < stdio.h> void mat_read( int mat[2][2] ) ; // Write these two functions for //yourselves void mat_print( int mat[2][2] ) ; void mat_add( int mat1[ ][2], int mat2[ ][2], int mat3[ ][2] ) ; void main() { int mat_a[2][2], mat_b[2][2], mat_res[2][2] ; puts( “Enter Matrix a row-wise :- ” ); mat_read( mat_a ) ; puts( “ Matrix a is :- ” ) ; mat_print( mat_a ) ; puts( “Enter Matrix b row-wise” ); mat_read( mat_b ) ; puts( “ Matrix b is :- ” ) ; mat_print( mat_b ) ; mat_add( mat_a, mat_b, mat_res ) ; puts( “The resultant matrix is ” ) ; mat_print( mat_res ) ; } void mat_add( int mat1[ ][2], int mat2[ ][2], int mat3[ ][2] ) { int j, k ; for ( j = 0; j < 2; j++ ) for ( k = 0; k < 2; k++ ) mat_res[j][k] = mat1[j][k] + mat2[j][k] ; }
All Rights Reserved. © 2024 BookOfNetwork