home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_09 / 8n09026a < prev    next >
Text File  |  1990-07-29  |  686b  |  36 lines

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define NUMELEM(a) (sizeof(a)/sizeof(a[0]))
  6.  
  7. main()
  8. {
  9.     int cmpstra(const void *, const void *);
  10.     char *array[] = {"abc", "Xy", "1234", "Abc", "xyz"};
  11.     int i;
  12.  
  13.     qsort(&array[0], NUMELEM(array), sizeof(char *), cmpstra);
  14.  
  15.     printf("ascending string order\n");
  16.     for (i = 0; i < NUMELEM(array); ++i)
  17.         printf("array[%d] = %s\n", i, array[i]);
  18.  
  19.     return 0;
  20. }
  21.  
  22. /* compare strings in ascending order */
  23.  
  24. int cmpstra(const void *pe1, const void *pe2)
  25. {
  26.     return strcmp(*(char **)pe1, *(char **)pe2);
  27. }
  28.  
  29. ascending string order
  30. array[0] = 1234
  31. array[1] = Abc
  32. array[2] = Xy
  33. array[3] = abc
  34. array[4] = xyz
  35.  
  36.