Friday, August 19, 2011

Passing one dimensional array to function

                   It is possible to pass an array to a function. While passing an array to a function, the called function does not know the size of the array and neither does the compiler so we need to explicitly pass array size as a separate argument so that the called function know the specific number of array elements to be processed.
E.g.
Int main()
{
Int x[10];
……………………
Void func1 (int  [ ], int);          //function declaration
……………………….                                 
Func1 (x,10);      //function call         
}                                        
Void func1 (int x[ ], int n)        //no semi colon
{                                             
……………
}
When an array is passed to a function, it is passed by reference and not by value which means that the elements of an array are not copied to the called function but the array name is interpreted as the address of its first element. Since the elements of an array are stored in contiguous memory location so the called function can access all the elements of the original array. Therefore, any changes made to array elements within the called function can be reflected in the original array.

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More