Single
operations which involve entire arrays are not permitted in C. The assignment
operations, comparison operations etc can not be carried in entire array;
instead it is carried out on an element by element basis. These operations are
usually accomplished using loop.
Passing arrays to
function
An entire array can be
passed to a function as an argument. To pass an array to a function, the array
name must appear by itself, without brackets or subscripts, as an actual
argument within the function call. The corresponding formal argument is written
in the same manner, though it must be declared as an array within the formal
argument declaration. When declaring a one-dimensional array as a formal
argument, the array name is written with a pair of empty square brackets. The
size of the array is not specified within the formal argument declaration.
Syntax for function call passing array as argument,
Function_name(array_name);
Syntax for function prototype which accepts array
Return_type
function_name (data_type array_name[]);
Example:
Write a program to read 10 numbers and reorders them in ascending order using function.
|
When an array is passed to a function, the values of
the array elements are not passed to the function. Rather, the array name is
interpreted as the address of the first array element. This address is assigned
to the corresponding formal argument when the function is called. The formal
argument therefore becomes a pointer to the first array element. Thus, array is
passed using call by reference. Thus, changing the values in function is also
recognized in main function.
An Examples:
|
|
Comments
Post a Comment