home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_07 / 9n07113a < prev    next >
Text File  |  1991-05-28  |  2KB  |  110 lines

  1.  
  2. /*
  3. *
  4. *    Program to illustrate use of typecasting with 'sort' and 
  5. *    'search' functions.
  6. *
  7. *    Compiled, linked & executed with both Turbo C++ 1.01
  8. *    and Turbo C2.01.
  9. *
  10. *    Sergio Cherskov     03/26/91
  11. */
  12.  
  13. #include  <stdlib.h>
  14. #include  <time.h>
  15. #include  <stdio.h>
  16.  
  17. /* just to stay generic in description */
  18. #define   ANY_LENGTH     34
  19. #define   ANY_SIZE       33
  20.  
  21. /* this solves qsort typecasting problem */
  22. typedef   int (*_QSORT_CMP_)(const void *, const void *);
  23.  
  24. typedef struct {
  25. int  len;
  26. int  ID;
  27. int  year;
  28. char data[ANY_LENGTH];
  29. } DATA;
  30.  
  31. #define   NELEM(array)   (sizeof(array)/sizeof(array[0]))
  32.  
  33. DATA data_array[ANY_SIZE];
  34.  
  35. void main(void);
  36. int       usr_fn1(DATA  *D1, DATA   *D2);
  37. int       usr_fn2(int *key,  DATA   *D);
  38. int  usr_fn3(int *key, DATA *D, int reserved, DATA*unused);
  39.  
  40. void main(void)
  41. {
  42. int       i;
  43. int       ID;
  44. DATA *D;
  45. è     for ( i=0; i < NELEM(data_array) ; i++  )  
  46.   {
  47.   data_array[i].ID = NELEM(data_array) -i; 
  48.           data_array[i].len = sizeof(DATA);
  49.   }
  50.  
  51. qsort(&data_array[0], NELEM(data_array), 
  52.   sizeof(DATA), (_QSORT_CMP_) usr_fn1 );
  53.  
  54.  printf("\nSearching with bsearch\n");
  55.  
  56.  /* Pick an item to search */
  57. ID = NELEM(data_array) / 3;
  58. D = bsearch(&ID, &data_array[0], NELEM(data_array), 
  59.    sizeof(DATA), (_QSORT_CMP_) usr_fn2 );
  60.  
  61. printf("Element with ID #%d, %sfound in the array.\n", 
  62.        ID, D ? "" : "NOT ");
  63.  
  64. /* check here with sequential search */
  65.  printf("Searching with straight search\n");
  66.  
  67. D = NULL;
  68. for ( i=0; i < NELEM(data_array) ; i++ ) 
  69.   {
  70.   if ( data_array[i].ID == ID ) 
  71.           {
  72.        D = &data_array[i];
  73.        break;
  74.        }
  75.    }
  76.  
  77. printf("Element with ID #%d, %sfound in the array.\n", 
  78.        ID, D ? "" : "NOT ", i);
  79.  
  80.  }
  81.  
  82. int usr_fn1(DATA *D1, DATA *D2)
  83. {
  84. return ( D1->ID - D2->ID );
  85. }
  86.  
  87. int usr_fn2(int *key, DATA *D)
  88. {
  89. return ( *key - D->ID  );
  90. }
  91.  
  92. int usr_fn3(int*key, DATA *D, int reserved, DATA *unused)
  93. {
  94. int       i;
  95.  
  96. /*
  97.  * The following statements just fool the comiler,
  98.  * and prevent warning that 'reserved' and 'unused'
  99.  * parameters were not used in function 'usr_fn3'.
  100.  */
  101.  
  102. i = unused->ID;
  103. i = reserved;
  104. i++;
  105.  
  106. return ( *key - D->ID );
  107. }
  108.  
  109.  
  110.