Reflection: ArrayAsParam
I, Summary
Arguments
Parameters
The syntax of a function header that receives an array address is
dataType functionIdentifier ( dataType arrayIdentifier [ ], ... )
The brackets following arrayIdentifier inform the compiler that the parameter holds the address of a one-dimensional array.
- Arrays are automatically passed by reference. Do not use &.
- If the function modifies the array, it is also modified in the calling environment
II, Applied to solve problems
Because compilers store the elements of an array contiguously in memory, passing the address of the array to a function is sufficient to enable access to all of the elements of the array within the function. The location of any element is simply the produce of the offset from the address of the first element and the number of bytes in a single element. By only passing the address of the array, we avoid copying the array, which may be a rather expensive operation.
Arguments
The syntax of a function call that passes an array is
functionIdentifier ( arrayIdentifier, ... )
Where functionIdentifier is the name of the function and arrayIdentifier is the array name without brackets.Parameters
The syntax of a function header that receives an array address is
dataType functionIdentifier ( dataType arrayIdentifier [ ], ... )
The brackets following arrayIdentifier inform the compiler that the parameter holds the address of a one-dimensional array.
- Arrays are automatically passed by reference. Do not use &.
- If the function modifies the array, it is also modified in the calling environment
I. Re-code again the example in the clip
This is my code
This is my result
II. Implement functions to solve problems in reflection array (as in clip array review)
arrayReview
III. Propose your own example to pass array as a parameter in a function
My example about sorting an array follow descending order. I use quicksort for this example.
sort.cpp
III, Some of my thoughtsIII. Propose your own example to pass array as a parameter in a function
My example about sorting an array follow descending order. I use quicksort for this example.
sort.cpp
Because compilers store the elements of an array contiguously in memory, passing the address of the array to a function is sufficient to enable access to all of the elements of the array within the function. The location of any element is simply the produce of the offset from the address of the first element and the number of bytes in a single element. By only passing the address of the array, we avoid copying the array, which may be a rather expensive operation.
Comments
Post a Comment