Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 5th, 2012  |  syntax: None  |  size: 2.26 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C linear search error
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define PF printf
  5. int main() {
  6.     int intcmp(void *ip1, void * ip2);
  7.     void * lsearch(void *key, void *base, int n, int elemSize,
  8.                    int(* cmpfun)(void *, void *));
  9.     int arr[] = {4, 6, 2, 3, 11, 22, 15};
  10.     int n = sizeof(arr) / sizeof(int);
  11.     int key = 11;
  12.     int *found = lsearch(&key, &arr, n, sizeof(int), intcmp);
  13.     PF("found=%p", found);
  14.     return 1;
  15. }
  16. int intcmp(void *ip1, void * ip2) {
  17.     int *p1 = ip1;
  18.     int *p2 = ip2;
  19.     return *p1 - *p2 == 0;
  20. }
  21. void * lsearch(void *key, void *base, int n, int elemSize,
  22.                int(* cmpfun)(void *, void *)) {
  23.     int i;
  24.     for(i = 0; i < n; i ++) {
  25.         void *elemArr = (char *)base + i * elemSize;
  26.         if(cmpfun(key, elemArr) == 0)
  27.             return elemArr;
  28.     }
  29.  
  30.     return NULL;
  31. }
  32.        
  33. if(cmpfun(key, elemArr) == 0)
  34.         return elemArr;
  35.        
  36. return *p1 - *p2 == 0;
  37.        
  38. #include <stdio.h>
  39. #include <string.h>
  40.  
  41. // This is bad practice. It makes your code less readable.
  42. // I won't use it below.
  43. #define PF printf
  44.  
  45. // Declare this first so a prototype is not needed.
  46. // You violated a C pattern by using `cmp` in the name.
  47. // Comparison functions in C return <0, 0, >0, not a binary value.
  48. // To wit, later you used the comparison correctly.  I've fixed the
  49. // inconsistency.
  50. int intcmp(void *vp1, void *vp2)
  51. {   // Most C styles have the brace on its own line, unlike Java. Roll with it.
  52.     int *p1 = vp1, *p2 = vp2;
  53.     return *p1 - *p2;
  54. }
  55.  
  56. // Search n elements of size elemSize in the array at
  57. // base in sequence using cmpfun on key,
  58. // to test for equality (cmpfun == 0).  Return a pointer
  59. // to the found element or NULL if none.
  60. void *lsearch(void *key, void *base, int n,
  61.               int elemSize, int(* cmpfun)(void *, void *))
  62. {
  63.     int i;
  64.  
  65.     for (i = 0; i < n; i++) {
  66.         void *elemArr = (char*)base + i * elemSize;
  67.         if (cmpfun(key, elemArr) == 0)
  68.             return elemArr;
  69.     }
  70.     return NULL;
  71. }
  72.  
  73. int main()
  74. {  
  75.     int arr[] = {4, 6, 2, 3, 11, 22, 15};
  76.     int n = sizeof(arr) / sizeof(int);
  77.     int key = 11;
  78.     int *found = lsearch(&key, &arr, n, sizeof(int), intcmp);
  79.     printf("found=%p (%d)", found, *(int*)found);
  80.     return 0; // By convention zero corresponds to no error.
  81. }