Guest User

Untitled

a guest
Jan 16th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. typedef struct DArray {
  2. int end;
  3. int max;
  4. size_t element_size;
  5. size_t expand_rate;
  6. void **contents;
  7. } DArray;
  8.  
  9. typedef int (*DArray_compare) (const void *a, const void *b);
  10.  
  11. int compare(const void *a, const void *b)
  12. {
  13. int i = (int)*a;
  14. int k = (int)*b;
  15. printf("%d %dn", i, k);
  16. return i - k;
  17. }
  18.  
  19. error: operand of type 'void' where arithmetic or pointer type is required int i = (int)*a;
  20.  
  21. int i = *(int*)a;
  22. // This one has more parens to make it really obvious what your intent is.
  23. int k = *((int*)b);
  24.  
  25. // Cast b from a pointer to a void into a pointer to an int.
  26. int *X = (int*)b;
  27. // k = "what X is pointing to" or "the contents of X"
  28. int k = *X;
  29.  
  30. typedef int (*DArray_compare) (const int *a, const int *b);
  31.  
  32. int compare(const int *a, const int *b)
  33. {
  34. int i = *a;
  35. int k = *b;
  36. ...
  37.  
  38. int compare(const void *a, const void *b)
  39. {
  40. const DArray *d1 = a;
  41. const DArray *d2 = b;
  42.  
  43. if (d1->end < d2->end)
  44. return -1;
  45. else if (d1->end > d2->end)
  46. return +1;
  47. else if (d1->max < d2->max)
  48. return -1;
  49. else if (d2->max > d2->max)
  50. return +1;
  51. else
  52. return 0;
  53. }
  54.  
  55. if (i < k)
  56. return -1;
  57. else if (i > k)
  58. return +1;
  59. else
  60. return 0;
Add Comment
Please, Sign In to add comment