W
walter.preuninger
Is there an easier way to code the cmp procedure without going thru all
the pointer manipulations?
#include <stdlib.h>
#include <string.h>
int cmp(const void *i, const void *j)
{
void *p1, *p2;
char **s1, **s2;
p1=(void *) i;
p2=(void *) j;
s1=(void *) p1;
s2=(void *) p2;
return strcmp(*s1,*s2);
}
int main(void)
{
char string0[]="zebra";
char string1[]="hello";
char string2[]="goodbye";
char *base[3];
base[0]=(char *)string0;
base[1]=(char *)string1;
base[2]=(char *)string2;
printf("\nbase[0] %s",base[0]);
printf("\nbase[1] %s",base[1]);
printf("\nbase[2] %s\n",base[2]);
qsort(&base,3,sizeof(base[0]),(void *)cmp);
printf("\nbase[0] %s",base[0]);
printf("\nbase[1] %s",base[1]);
printf("\nbase[2] %s\n",base[2]);
}
TIA,
Walter
the pointer manipulations?
#include <stdlib.h>
#include <string.h>
int cmp(const void *i, const void *j)
{
void *p1, *p2;
char **s1, **s2;
p1=(void *) i;
p2=(void *) j;
s1=(void *) p1;
s2=(void *) p2;
return strcmp(*s1,*s2);
}
int main(void)
{
char string0[]="zebra";
char string1[]="hello";
char string2[]="goodbye";
char *base[3];
base[0]=(char *)string0;
base[1]=(char *)string1;
base[2]=(char *)string2;
printf("\nbase[0] %s",base[0]);
printf("\nbase[1] %s",base[1]);
printf("\nbase[2] %s\n",base[2]);
qsort(&base,3,sizeof(base[0]),(void *)cmp);
printf("\nbase[0] %s",base[0]);
printf("\nbase[1] %s",base[1]);
printf("\nbase[2] %s\n",base[2]);
}
TIA,
Walter