help with correcting compiler errors

could someone help me correct my errors.

heres my code for the definition of a function:

1
2
3
4
5
6
7
void sortKickers(MyKicker kickers[], int count)
{
	if ( strcmp( kickers[ count - 1 ].name , kickers[ count ].name ) > 0 ) 
	{
    	swapKickers( kickers[ count - 1 ], kickers[ count ], count ); 
    }
}


and heres my code for the definition of the function 'swapKickers' inside of the sortKickers function:

1
2
3
4
5
6
7
void swapKickers(MyKicker kickers[count-1], MyKicker kickers[count], int count)
{
	MyKicker temp;
	temp = kickers[count];
	kickers[count] = kickers[count-1];
	kickers[count-1] = temp;
}


MyKicker is an array of structs. any ideas?

thanks
Last edited on
Welcome to the forum, please use the Format tags in the palette on the right to format your code.

Arrays in C and C++ are passed by reference, never by value.

Plase don't do this:
 
int main(const int argc, const char* argv[])
it shoudl be:
 
int main(const int argc, char* argv[])
thanks for the info. i just formatted my code. could someone help me correct my errors please?
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 ] ); 
	}
}
Topic archived. No new replies allowed.