home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / dtx9202 / borhot / qsortex.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-28  |  1.8 KB  |  53 lines

  1. /* ------------------------------------------------------ */
  2. /*                    QSORTEX.C                           */
  3. /*       Modifying Online qsort(). Example for C++.       */
  4. /*            (c) 1990 Borland International              */
  5. /*                 All rights reserved.                   */
  6. /* ------------------------------------------------------ */
  7. /*  veröffentlicht in: DOS toolbox 1'91/92                */
  8. /* ------------------------------------------------------ */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. // Add this user defined type
  14.  
  15. typedef int (*sortFn) (const void*, const void*);
  16.  
  17. // Change sort_function to receive char* arguments
  18.  
  19. int sort_function(const char *a, const char *b);
  20.  
  21. char list[5][4] = { "cat", "car", "cab", "cap", "can" };
  22.  
  23. // ------------------------------------------------------- *
  24. int main(void)
  25. {
  26.   int x;
  27.  
  28.   // Use new sortFn user defined type to defeat
  29.   // mismatch error
  30.  
  31.   qsort((void *)list, 5, sizeof(list[0]),
  32.                 (sortFn) sort_function);
  33.  
  34.   for (x = 0; x < 5; x++)
  35.     printf("%s\n", list[x]);
  36.   return 0;
  37. }  // end of main()
  38.  
  39. /* ------------------------------------------------------ */
  40. /*  NOTE: Due to C++'s stronger type checking, an error   */
  41. /*        will be reported on the strcmp() line unless    */
  42. /*        the const void parameters in the                */
  43. /*        sort_function() signature are changed to const  */
  44. /*        char* as required by strcmp()'s prototype.      */
  45. /* ------------------------------------------------------ */
  46. int sort_function(const char *a, const char *b)
  47. {
  48.   return(strcmp(a, b));
  49. }
  50. /* ------------------------------------------------------ */
  51. /*                 Ende von QSORTEX.CPP                   */
  52.  
  53.