Close.
The 'array' is a pointer to an array. More specifically to first element of the array. The array has 'size' elements.
Dereferencing the pointer we can reach the individual elements / their values.
For example,
array[0]
is the first element
and
array[size-1]
is the
last element.
Who is
array[size]
then? An out of range error, for we have no idea what is in that memory.
You, however, do not access that non-existent element in main(). You merely take its address
&array[size]
. That too could be a violation.
There is an another, "pointer math" syntax for dereferencing an element:
*(array+size)
Taking address is thus:
&(*(array+size))
But why dereference and then take address, when the address is already there before referencing:
array+size
What goes wrong?
You give address
array+size
to a function that thinks that the address is of first element in size-long array. It is not. The first element of the array that you have allocated is at
array
.
Rewrite line 55:
sortAscend( array, size );
Notes:
* You have two loops in main(). Both loop through the array. Why do they use different loop counter?
*
NULL
is old macro. Current C++ has
nullptr
that has subtle benefits.
*
1 2 3
|
int sortAscend(int arraySort[], int sortSize)
// means same as
int sortAscend(int * arraySort, int sortSize)
|