home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / tctnt / qsortex.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  1.1 KB  |  34 lines

  1. /* QSORTEX.CPP: Modifying Online qsort() Example for C++ */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. // Add this user defined type
  8. typedef int (*sortFn)(const void*, const void*);
  9.  
  10. // Change sort_function to receive char* arguments
  11. int sort_function(const char *a, const char *b);
  12.  
  13. char list[5][4] = { "cat", "car", "cab", "cap", "can" };
  14.  
  15. //*******************************************************************
  16. int main(void) {
  17.   int  x;
  18.  
  19.   // Use new sortFn user defined type to defeat mismatch error
  20.   qsort((void *)list, 5, sizeof(list[0]), (sortFn)sort_function);
  21.  
  22.   for (x = 0; x < 5; x++)
  23.     printf("%s\n", list[x]);
  24.   return 0; 
  25. }  // end of main()
  26.  
  27. /*-------------------------------------------------------------------
  28.    NOTE: Due to C++'s stronger type checking, an error will be
  29.          reported on the strcmp() line unless the const void*
  30.          parameters in the sort_function() signature are changed
  31.          to const char* as required by strcmp()'s prototype. */
  32. int sort_function(const char *a, const char *b) {
  33.   return(strcmp(a,b)); }
  34.