< previous page page_977 next page >

Page 977
    float velocity[30];
      .
      .
      .
    ZeroOut(velocity, 30);
      .
      .
      .
}
In the function call, the first parameteran array name without index bracketsis a pointer expression. The value of this expression is the base address of the velocity array. This base address is passed to the function. We can write the ZeroOut function in one of two ways. The first approachone that you have seen many timesdeclares the first formal parameter to be an array of unspecified size.
void ZeroOut( /* out */ float arr[],
              /* in */  int   size )
{
    int i;

    for (i = 0; i < size; i++)
        arr[i] = 0.0;
}
Alternatively, we can declare the formal parameter to be of type float*, because the parameter simply holds the address of a float variable (the address of the first array element).
void ZeroOut( /* out */ float* arr,
              /* in */  int    size )
{
    .
    .
    .   // Function body is unchanged
}
Whether we declare the formal parameter as float arr[] or as float*arr, the result is exactly the same to the C++ compiler: within the ZeroOut function, arr is a simple variable that points to the beginning of the caller's actual array (see Figure 17-5).
Even though arr is a pointer variable within the ZeroOut function, we are still allowed to attach an index expression to the name arr:
arr[i] = 0.0;

 
< previous page page_977 next page >