fork download
  1. #include <stdio.h>
  2. #define BYPASS NULL
  3. typedef void(*callback)(int key,void* val);
  4. typedef int(*predicate)(void* elem1,void* elem2);
  5.  
  6.  
  7. void call_where(
  8. void *arr,
  9. int elemCount,
  10. int elemSize,
  11. callback call,
  12. predicate when,
  13. void* elem
  14. )
  15. {
  16. int i;
  17. for(i = 0; i < elemCount; i++)
  18. if(when == BYPASS
  19. || when(elem, (char*)arr + i * elemSize) )
  20. call(i, (char*)arr + i * elemSize);
  21. }
  22.  
  23.  
  24.  
  25. void IntRandom(int key, void* val)
  26. {
  27. *(int*)val = rand()%10;
  28. }
  29. void IntPrint(int key, void* val)
  30. {
  31. printf("%d => %d\n",key,*(int*)val);
  32. }
  33. int IntEquals(void* a, void* b)
  34. {
  35. return *(int*)a == *(int*)b;
  36. }
  37. int main()
  38. {
  39.  
  40. int arr[50];
  41. int searchFor = 3;
  42. call_where(arr,50,sizeof(int),IntRandom,BYPASS,BYPASS);
  43. printf("Init Array\n");
  44. call_where(arr,50,sizeof(int),IntPrint,BYPASS,BYPASS);
  45. printf("Found 3 at\n");
  46. call_where(arr,50,sizeof(int),IntPrint,IntEquals,&searchFor);
  47.  
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
Init Array
0 => 3
1 => 6
2 => 7
3 => 5
4 => 3
5 => 5
6 => 6
7 => 2
8 => 9
9 => 1
10 => 2
11 => 7
12 => 0
13 => 9
14 => 3
15 => 6
16 => 0
17 => 6
18 => 2
19 => 6
20 => 1
21 => 8
22 => 7
23 => 9
24 => 2
25 => 0
26 => 2
27 => 3
28 => 7
29 => 5
30 => 9
31 => 2
32 => 2
33 => 8
34 => 9
35 => 7
36 => 3
37 => 6
38 => 1
39 => 2
40 => 9
41 => 3
42 => 1
43 => 9
44 => 4
45 => 7
46 => 8
47 => 4
48 => 5
49 => 0
Found 3 at
0 => 3
4 => 3
14 => 3
27 => 3
36 => 3
41 => 3