You can declare a built-in array parameter in a function header, as follows:
int sumElements( const int values[], const size_t numberOfElements )
which indicates that the function’s first argument should be a one-dimensional built-in array of int
s that should not be modified by the function. Unlike array
objects, built-in arrays don’t know their own size, so a function that processes a built-in array should have parameters to receive both the built-in array and its size.
The preceding header can also be written as:
int sumElements( const int *values, const size_t numberOfElements )
The compiler does not differentiate between a function that receives a pointer and a function that receives a built-in array. This, of course, means that the function must “know” when it’s receiving a built-in array or simply a single variable that’s being passed by reference. When the compiler encounters a function parameter for a one-dimensional built-in array of the form const int values[]
, the compiler converts the parameter to the pointer notation const int *values
(that is, “values
is a pointer to an integer constant”). These forms of declaring a one-dimensional built-in array parameter are interchangeable—for clarity you should use the []
notation when the function expects a built-in array argument.
18.119.163.238