(E-Mail Removed) wrote:

>

> 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]);

> }
/* BEGIN new.c */

#include <stdlib.h>

#include <string.h>

#include <stdio.h>

int cmp(const void *i, const void *j)

{

return strcmp(i, j);

}

int main(void)

{

char string0[] = "zebra";

char string1[] = "hello";

char string2[] = "goodbye";

char *base[3];

base[0] = string0;

base[1] = string1;

base[2] = string2;

printf("base[0] %s\n", base[0]);

printf("base[1] %s\n", base[1]);

printf("base[2] %s\n", base[2]);

putchar('\n');

qsort(base, sizeof base / sizeof*base, sizeof*base, cmp);

printf("base[0] %s\n",base[0]);

printf("base[1] %s\n",base[1]);

printf("base[2] %s\n",base[2]);

return 0;

}

/* END new.c */

--

pete