Coupon Accepted Successfully!


Initialization Of Arrays Using Functions

The programmers always initialize the arrays using statements such as int d[] = {1,2,3,4,5}; instead of this, the function can also be directly called to initialize the array. The following program illustrates this point.

12.10 Write a program to initialize an array using functions.





int k,c(),d[]={c(),c(),c(),c(),c()};

printf (“\n Array d[] elements are :”);
for (k=0;k<5;k++)
printf (“%2d”,d[k]);
return (NULL);


static int m,n;
printf (“\nEnter Number d[%d] : ”,m);
scanf (“%d”,&n);



Enter Number d[1] : 4

Enter Number d[2] : 5

Enter Number d[3] : 6

Enter Number d[4] : 7

Enter Number d[5] : 8

Array d[] elements are : 4 5 6 7 8

A function can be called in the declaration of an array. In the above program, d[] is an integer array, and c() is a user-defined function. When called, the function c() reads values through the keyboard. The function c() is called from an array; that is, the values returned by the function are assigned to the array. The above program will not work in C.

Test Your Skills Now!
Take a Quiz now
Reviewer Name