fork download
  1. // see http://stackoverflow.com/q/30603685/228539
  2.  
  3. // Note that this does not address the issue of later on knowing
  4. // how long the array actually is so you can iterate over it.
  5.  
  6. // Also note the concerns raised about memory safety in the above
  7. // StackOverflow answers and comments.
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <stdint.h>
  12.  
  13. typedef struct T_Function T_Function;
  14. typedef T_Function * T_Inhibits[];
  15.  
  16. struct T_Function
  17. {
  18. T_Inhibits * inhibitsTD; // pointer to array of pointers to this structure
  19. T_Function * (* inhibits)[]; // pointer to array of pointers to this structure
  20. };
  21.  
  22. #define T_Function_Default(...) \
  23. { \
  24. .inhibitsTD = NULL, \
  25. __VA_ARGS__ \
  26. }
  27.  
  28. typedef struct
  29. {
  30. int i;
  31. float f;
  32. } T_Simple;
  33.  
  34. T_Simple * s = & (T_Simple) {.i = 42, .f = 3.14};
  35.  
  36. typedef int T_Array4[4];
  37. T_Array4 * pa4 = & (T_Array4) {1,2,3,4};
  38.  
  39. typedef int * T_Array4p[4];
  40. int one, two, three, four;
  41. T_Array4p * pa4p = & (T_Array4p) {&one,&two,&three,&four};
  42. int * (* not_typed)[4] = & (int *[]) {&one,&two,&three,&four};
  43.  
  44. int main(void) {
  45. T_Function clutch = T_Function_Default();
  46. T_Function start = T_Function_Default();
  47. T_Function cruise = T_Function_Default();
  48. T_Function radio = T_Function_Default();
  49.  
  50. // With an array variable to take the address of
  51. T_Function a, b, c;
  52. T_Function * ai[] = {&b, &c};
  53. a.inhibitsTD = &ai;
  54. b = a; // just to avoid unused errors
  55.  
  56. // Taking the address of the compound literal
  57. T_Function x;
  58. // direct
  59. x.inhibitsTD = &(T_Function *[]) {&x};
  60. x.inhibits = &(T_Function *[]) {&x};
  61. // via typedef
  62. x.inhibitsTD = &(T_Inhibits) {&x};
  63. x.inhibits = &(T_Inhibits) {&x};
  64.  
  65. clutch.inhibitsTD = &(T_Inhibits) {&start};
  66. start.inhibitsTD = &(T_Inhibits) {&radio, &cruise};
  67.  
  68. start = clutch; // just to avoid unused errors
  69. clutch = start; // just to avoid unused errors
  70.  
  71. return 0;
  72. }
  73.  
Success #stdin #stdout 0s 2244KB
stdin
Standard input is empty
stdout
Standard output is empty