sort
Moderator: Board moderators
-
- Experienced poster
- Posts: 147
- Joined: Fri Jun 13, 2003 10:46 pm
sort
how do u sort character arrays in C
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm
-
- Experienced poster
- Posts: 154
- Joined: Sat Apr 17, 2004 9:34 am
- Location: EEE, BUET
Well to be accurate the sort function should be written as:
[c]
#include <stdlib.h>
struct m
{
int x, y;
};
int cmp( const void *l, const void *r )
{
struct m *a = (struct m *)l;
struct m *b = (struct m *)r;
if ( a->x > b-x )
return 1;
if ( b->x > a->x )
return -1;
return 0;
}
int main( void )
{
struct m A[10];
qsort(A, 10, sizeof(m), cmp);
return 0;
}
[/c]
Otherwise the compiler will complain, and your sort function might not work as expected.
Regards,
Suman.
[c]
#include <stdlib.h>
struct m
{
int x, y;
};
int cmp( const void *l, const void *r )
{
struct m *a = (struct m *)l;
struct m *b = (struct m *)r;
if ( a->x > b-x )
return 1;
if ( b->x > a->x )
return -1;
return 0;
}
int main( void )
{
struct m A[10];
qsort(A, 10, sizeof(m), cmp);
return 0;
}
[/c]
Otherwise the compiler will complain, and your sort function might not work as expected.
Regards,
Suman.
Last edited by sumankar on Fri Dec 17, 2004 6:08 am, edited 1 time in total.