fork download
  1. #include <stdio.h>
  2. #include <stddef.h>
  3.  
  4. #define LENGTH(ARRAY) (sizeof(ARRAY)/sizeof((ARRAY)[0]))
  5.  
  6. #define arr(NAME, TYPE, ...) \
  7. TYPE *NAME =\
  8. ((struct {\
  9. size_t n;\
  10. TYPE u[LENGTH(((TYPE []){__VA_ARGS__}))];\
  11. }){\
  12. .n = LENGTH(((TYPE []){__VA_ARGS__})),\
  13. .u = {__VA_ARGS__}\
  14. }).u
  15.  
  16. #define len(ARRAY) \
  17. *(size_t *)(void *)((char *)&ARRAY[0]-offsetof(struct {size_t n; void *u;}, u))
  18.  
  19. struct xy {
  20. int x;
  21. int y;
  22. };
  23.  
  24. static void print_array(struct xy *a)
  25. {
  26. for (size_t i = 0; i < len(a); ++i) {
  27. printf("(%d, %d) ", a[i].x, a[i].y);
  28. }
  29. printf("\nThe size of this array is %zu\n", len(a));
  30. return;
  31. }
  32.  
  33. int main(void)
  34. {
  35. arr(points, struct xy, {12, 34}, {56, 78}, {11, 99});
  36. print_array(points);
  37. return 0;
  38. }
Success #stdin #stdout 0s 2168KB
stdin
Standard input is empty
stdout
(12, 34) (56, 78) (11, 99) 
The size of this array is 3