, le 08/03/2006 a écrit :
> Is there an easier way to code the cmp procedure without going thru all
> the pointer manipulations?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define AFFICHE printf("\nbase[0] %s""\nbase[1] %s""\nbase[2] %s\n",\
base[0],\
base[1],\
base[2])
typedef char* chaine;
typedef int(*f_comp_t)(const void*, const void*);
int cmp_chaine(const chaine* i, const chaine* j)
{
return strcmp(*i,*j);
}
int main(void)
{
char string0[]="zebra", string1[]="hello", string2[]="goodbye";
chaine base[] = {string0, string1, string2};
AFFICHE;
qsort(base,3,sizeof(chaine),(f_comp_t)cmp_chaine);
AFFICHE;
return 0;
}
Or:
/* .... */
int main(void)
{
char string0[]="zebra", string1[]="hello", string2[]="goodbye";
chaine base[] = {string0, string1, string2};
f_comp_t cmp = (f_comp_t)cmp_chaine;
AFFICHE;
qsort(base,3,sizeof(chaine),cmp);
AFFICHE;
return 0;
}
--
Pierre Maurette