C Tutorial/Array/Array Parameter
Pass array to function
<source lang="cpp">#include<stdio.h>
#include<conio.h> void read(int *,int); void dis(int *,int); void main() { int a[5],b[5],c[5],i; printf("Enter the elements of first list \n"); read(a,5); printf("The elements of first list are \n"); dis(a,5); } void read(int c[],int i) { int j; for(j=0;j<i;j++) scanf("%d",&c[j]); fflush(stdin); } void dis(int d[],int i) { int j; for(j=0;j<i;j++) printf("%d ",d[j]); printf("\n"); }</source>
Enter the elements of first list 1 2 3 4 5 The elements of first list are 1 2 3 4 5
Passing arrays and individual array elements to functions
<source lang="cpp">#include <stdio.h>
- define SIZE 5
void modifyArray( int b[], int size ); void modifyElement( int e ); int main() {
int a[ SIZE ] = { 0, 1, 2, 3, 4 }; int i; printf( "Effects of passing entire array by reference:\n\nThe " "values of the original array are:\n" ); for ( i = 0; i < SIZE; i++ ) { printf( "%3d", a[ i ] ); } printf( "\n" ); modifyArray( a, SIZE ); printf( "The values of the modified array are:\n" ); for ( i = 0; i < SIZE; i++ ) { printf( "%3d", a[ i ] ); } printf( "\n\n\nEffects of passing array element " "by value:\n\nThe value of a[3] is %d\n", a[ 3 ] ); modifyElement( a[ 3 ] ); printf( "The value of a[ 3 ] is %d\n", a[ 3 ] ); return 0;
} void modifyArray( int b[], int size ) {
int j; for ( j = 0; j < size; j++ ) { b[ j ] *= 2; }
} void modifyElement( int e ) {
printf( "Value in modifyElement is %d\n", e *= 2 );
}</source>
Effects of passing entire array by reference: The values of the original array are: 0 1 2 3 4 The values of the modified array are: 0 2 4 6 8 Effects of passing array element by value: The value of a[3] is 6 Value in modifyElement is 12 The value of a[ 3 ] is 6