can anyone kind enough to point out my bug here.
-
i am trying to use qsort so that i can sort out y from highest to lowest. can anyone help me? anyways, thank you very much. struct SPRITE { int x; int y; }; // Maximum number of sprites const int N_SPRITES = 10; // Array of sprites SPRITE sprites[N_SPRITES]; int Compare (const void *one, const void *two); /* Pre: Enter value for one and two * Post: Compares all of both strings */ int main() { int value; for (int i=0; i> value; sprites[i].y = value; } qsort(sprites, N_SPRITES, sizeof(int), Compare); for (i=0; i
-
i am trying to use qsort so that i can sort out y from highest to lowest. can anyone help me? anyways, thank you very much. struct SPRITE { int x; int y; }; // Maximum number of sprites const int N_SPRITES = 10; // Array of sprites SPRITE sprites[N_SPRITES]; int Compare (const void *one, const void *two); /* Pre: Enter value for one and two * Post: Compares all of both strings */ int main() { int value; for (int i=0; i> value; sprites[i].y = value; } qsort(sprites, N_SPRITES, sizeof(int), Compare); for (i=0; i
for (int i=0; i // ERROR { ... } for ( int i=0 ; i < N_SPRITES ; i++ ) // GOOD { ... } Aizik Yair Software Engineer
-
i am trying to use qsort so that i can sort out y from highest to lowest. can anyone help me? anyways, thank you very much. struct SPRITE { int x; int y; }; // Maximum number of sprites const int N_SPRITES = 10; // Array of sprites SPRITE sprites[N_SPRITES]; int Compare (const void *one, const void *two); /* Pre: Enter value for one and two * Post: Compares all of both strings */ int main() { int value; for (int i=0; i> value; sprites[i].y = value; } qsort(sprites, N_SPRITES, sizeof(int), Compare); for (i=0; i
First: John Cruz wrote: qsort(sprites, N_SPRITES, sizeof(int), Compare); should be
qsort(sprites, N_SPRITES, sizeof(SPRITE), Compare);
Second: John Cruz wrote: int Compare (const void *one, const void *two) { return strcmp((char*)one, (char*)two); } one and two are pointing to SPRITE objects. So it should be something like:return strcmp(((SPRITE*)one).y, ((SPRITE*)two).y);
that is if the y member is a char*. -
First: John Cruz wrote: qsort(sprites, N_SPRITES, sizeof(int), Compare); should be
qsort(sprites, N_SPRITES, sizeof(SPRITE), Compare);
Second: John Cruz wrote: int Compare (const void *one, const void *two) { return strcmp((char*)one, (char*)two); } one and two are pointing to SPRITE objects. So it should be something like:return strcmp(((SPRITE*)one).y, ((SPRITE*)two).y);
that is if the y member is a char*.i got your number my first mistake fixed. but the second mistake: Niklas Lindquist wrote: one and two are pointing to SPRITE objects. So it should be something like: return strcmp(((SPRITE*)one).y, ((SPRITE*)two).y); that is if the y member is a char*. this wont compile because i am using strcmp which has needs 2 const char parameters and because y is not a member of char. does any know how to fix this thing? well anyways, thank you very much. Thank you very much, John :-D Aloha from Hawaii :-)