fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct mystuff {
  5. int a;
  6. int b;
  7. int c;
  8. };
  9.  
  10. #define declare_getstuff_for(f) int* get_mystuff_##f(struct mystuff *array, int n) \
  11. { \
  12.   int i; \
  13.   int *a_array; \
  14. \
  15.   a_array = malloc(n*sizeof(int)); \
  16. \
  17.   for(i=0;i<n;i++) { \
  18.   a_array[i] = array[i].f; \
  19.   } \
  20.   return a_array; \
  21. }
  22.  
  23. declare_getstuff_for(a)
  24. declare_getstuff_for(b)
  25. declare_getstuff_for(c)
  26.  
  27.  
  28. int main(void) {
  29. int *result1, *result2, *result3;
  30. struct mystuff array[3];
  31. struct mystuff el1, el2, el3;
  32. el1.a = el1.b = el1.c = 1;
  33. el2.a = el2.b = el2.c = 5;
  34. el3.a = el3.b = el3.c = 3;
  35.  
  36. array[0] = el1;
  37. array[1] = el2;
  38. array[2] = el3;
  39.  
  40. result1 = get_mystuff_a(array, 3);
  41. printf("%d, %d, %d\n", result1[0], result1[1], result1[2]);
  42.  
  43. result2 = get_mystuff_b(array, 3);
  44. printf("%d, %d, %d\n", result2[0], result2[1], result2[2]);
  45.  
  46. result3 = get_mystuff_c(array, 3);
  47. printf("%d, %d, %d\n", result3[0], result3[1], result3[2]);
  48.  
  49. // Now free your memory :)
  50.  
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
1, 5, 3
1, 5, 3
1, 5, 3