In C, function arguments are usually passed by value. However, it is not possible to directly* (see footnote) pass an array to a function by value. Passing an array to a function is usually done by passing a pointer to the first element of the array. Only this pointer is then passed by value.
However, it is still allowed to specify an array as a function parameter, as you are doing:
void greatestOf( int arr[], int *result )
But when you do this, the array will decay to a pointer, so the declaration is equivalent to the following:
void greatestOf( int *arr, int *result )
Therefore, there is nothing wrong with how you are declaring the function parameters. However, there is something wrong with how you are calling the function. You are calling it like this:
greatestOf(arr[4], &x);
As previously stated, arrays are usually passed to functions by passing a pointer to the first element of the array. Therefore, the correct way would be to call it like this:
greatestOf( &arr[0], &x );
However, it is more common to simply write:
greatestOf( arr, &x );
This is possible because when you pass an array to a function, the array will automatically decay to a pointer to the first element of the array, which is &arr[0]
.
By writing arr[4]
instead, you are passing the value of the (non-existant) 5th element of the array (array indexes are 0-based) to the function. This is wrong, because you need to pass a pointer to the first element.
Note that when declaring an array, you must specify the size of the array in []
, but when used outside an expression, the []
has a completely different meaning: It means that you index into the array.
I think if want to send value to function I have to include the size of the array
There is no need to pass the size of the array to the function, if the size of the array is guaranteed to always be 4
. However, in that case, I suggest that you change the function declaration
void greatestOf( int arr[], int *result )
to
void greatestOf( int arr[4], int *result )
in order to make clear to anybody reading the code that the function takes a pointer to an array whose length is 4
. The 4
will be ignored by the compiler, because, as previously stated, the array declaration will decay to a pointer, in this case int *arr
.
You can also write
void greatestOf( int arr[static 4], int *result )
which will tell the compiler that the parameter arr
is guaranteed to be a valid pointer (e.g. non-NULL
) to an array which is guaranteed to be at least 4
elements long. Doing this may improve performance.
If you want the function greatestOf
to also be able to handle arrays that have a size different than 4
, you can write the function like this:
int greatestOf( int arr[], size_t length )
{
int max;
if ( length == 0 )
{
fprintf( stderr, "Invalid array size!\n" );
exit( EXIT_FAILURE );
}
max = arr[0];
for ( size_t i = 1; i < length; i++ )
{
if ( arr[i] > max )
{
max = arr[i];
}
}
return max;
}
Note that this code requires you to add #include <stdlib.h>
to the top of the source file.
In the function main
, instead of calling the function main
like this
greatestOf(arr, &x);
you can now call it like this:
x = greatestOf( arr, sizeof arr / sizeof *arr );
The expresson sizeof arr / sizeof *arr
is the size (in bytes) of the entire array divided by the size of a single array element. Therefore, this expression will evaluate to the number of elements in the array.
Footnote:
* It is possible to pass an array to a function by value, if it is enclosed in a struct
. However, passing arrays by value is usually not recommended, because it creates a copy of the entire array, which can be bad for performance.