Loading....
Coupon Accepted Successfully!

 

Passing Complete Array Elements To A Function

It is also possible to pass the entire elements of an array to a function instead of passing the individual elements of an array. The following program explains the concept:
 

12.9 Program to pass entire elements of an array to a function.

#include<iostream.h>

#include<conio.h>

void show(int *,int);

void main()

{

int num[5]={1,2,3,4,5},i;

clrscr();

cout<<“\nElements of the array are as follows:”;

show(&num[0],5);

}

void show(int *x,int y)

{

int i;

for(i=0;i<=y-1;i++)

{

cout<<“ ”<<*x;

x++;

}

}

OUTPUT
Elements of the array are as follows: 1 2 3 4 5

Explanation:
 In the above program, the address of the zeroth element, that is, &num[0], is passed to the function show(). The for loop is used to access the array elements using pointers. The show() function is invoked with two arguments. The first argument is the address of the zeroth element, and the second one is the number that represents the total number of elements in the array.




Test Your Skills Now!
Take a Quiz now
Reviewer Name