-
pointers.
i've been doing revision on pointers, and....well i'll show you the codes.
in one function i've got it declared as:
int findIndexMin(int *arr, ........)
and thats an array, however inside of that function when refferring to the array, it only uses "arr" without the "*".
However in a different code:
void UpdatePurchase(float *accountPtr........)
inside of this function, anything that is referring to the account pointer is shown as "*accountPtr" with the "*" there.
Is there a reason for this??
-
Yup :)
When you pass an array to a function you are passing a pointer to it's initial argument:
int findIndexMin(int *arr)
int findIndexMin(int arr[])
Both are in fact about the same thing. The thing to remember here is that on the second case even though you are not using pointer notation, the array will still be converted to a pointer.
Now the tricky part is that an array that was passed as an argument to a function need not be used with pointer notation (no matter what the above methods you used). It's is my opinion that for the sake of consistency the first form above is the most correct one (after all, the array will be converted to a pointer no matter what). It is also my opinion that inside the function you should be then using pointer notation when referring to the array.
The authour of your code choose to use pointer notation for the header of the function and not for the body. This is fine and a property of the arrays as I described above. But it's not logically consistent with proper pointer grammar.
Just a final note: When you pass a pointer as an argument something change with the usual pass by value mechanism. And this obviously as a strong effect on arrays as they are converted to pointers wether you wish or not.
Pass by value, as you know, creates a copy of an argument and uses that copy throughout the function body, leaving the original unchanged. But when you pass a pointer you are indeed passing the memory address of that variable. So, it still creates a copy... but a copy to a memory address. This means the copy still points to the original variable. So when changing an element of the copy array, you will indeed be changing the original element. IF you wish to enforce the fact that you are not going to change the array you can declare the argument in the following manner:
int findIndexMin(const int *arr)