C/C++ treat arrays a little differently. There's a historical reason, but that's another story.
When you pass an array to a function, the address of the first element is passed. I repeat, when you pass an array to a function, a pointer is passed.
You can declare the function as
|
void sortKickers(MyKicker kickers[], int count)
|
or
|
void sortKickers(MyKicker* kickers, int count)
|
[] is an incomplete type, but it resolves to *, so they're equivalent.
You can't rely on the size of the array. Remember, just a pointer is passed. That's why the size must also be passed.
The swap function isn't processing an array, it's just swapping values that ought to be in an array. To a C swap function would take pointers to the values, whereas C++ gives you the option of passing the values by reference. In fact, there's standard swap function that you can use.
Putting it together, using C, you have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void swapKickers(MyKicker* a, MyKicker* b)
{
MyKicker temp;
temp = *a;
*a = *b;
*b = temp;
}
void sortKickers(MyKicker* kickers, size_t count)
{
if ( strcmp( kickers[ count - 1 ].name , kickers[ count ].name ) > 0 )
{
swapKickers( &kickers[ count - 1 ], &kickers[ count ] );
}
}
|