Pointers & Functions - PASSING ARRAY TO A FUNCTION
Now we will see how to pass numeric array to function,
/* Program to read and print array elements */
# include < stdio.h >
ArrayRead(p,n)
int *p,n;
{
int i;
printf("\nEnter the array elements ");
for(i=0;i {
scanf("%d",* ( p + i ) );
}
}
ArrayPrint ( p, n )
int *p , n;
{
int i;
printf("\nThe array elements ");
for(i=0;i {
printf("%3d", * ( p + i ) );
}
}
main ( )
{
int a[50] , n;
printf ( " \n Enter the size of array " );
scanf ( "%d", &n );
/* calling ArrayRead ( ) function, observe we are passing address of first element of array just by writing array name */
ArrayRead ( a, n);
/* calling ArrayPrint ( ) function, observe we are passing address of first element of array by writing &a[0] */
ArrayPrint ( &a[0], n );
}
In this topic we will see how to pass an array to function. As we already know that simply writing array name or &a[0] will represent the address of first element of array(Refer http://sridhanvantari.blogspot.com/2013/12/pointers-6-pointers-arrays.html ). From calling function pass this address to called function. In function, the receiver of this address must be a pointer.
Now we will see how to pass numeric array to function,
/* Program to read and print array elements */
# include < stdio.h >
ArrayRead(p,n)
int *p,n;
{
int i;
printf("\nEnter the array elements ");
for(i=0;i
scanf("%d",* ( p + i ) );
}
}
ArrayPrint ( p, n )
int *p , n;
{
int i;
printf("\nThe array elements ");
for(i=0;i
printf("%3d",
}
}
main ( )
{
int a[50] , n;
printf ( " \n Enter the size of array " );
scanf ( "%d", &n );
/* calling ArrayRead ( ) function, observe we are passing address of first element of array just by writing array name */
ArrayRead ( a, n);
/* calling ArrayPrint ( ) function, observe we are passing address of first element of array by writing &a[0] */
ArrayPrint ( &a[0], n );
}
Comments
Post a Comment