fork(1) download
  1. #include <stdio.h>
  2.  
  3. #define ASIZE(a) (sizeof (a) / sizeof((a)[0]))
  4.  
  5. #define CHECK_ARRAY(a) ((void)(0&&((int (*)(__typeof__(a[0])(*)[ASIZE(a)]))NULL)(&(a))))
  6.  
  7. #define ASIZE_SAFE(a) (CHECK_ARRAY(a), ASIZE(a))
  8.  
  9. int main(void)
  10. {
  11. short a[3];
  12. short *b;
  13. int c[2];
  14. int *d;
  15. long long e[5][4];
  16. char *f[4];
  17. char (*g)[4];
  18. (void)a; (void)b; (void)c; (void)d; (void)e; (void)f; (void)g;
  19. printf("ASIZE() accepts pointers, producing invalid results.\n");
  20. printf("%zu\n", ASIZE( a ));
  21. printf("%zu\n", ASIZE( b ));
  22. printf("%zu\n", ASIZE( c ));
  23. printf("%zu\n", ASIZE( d ));
  24. printf("%zu\n", ASIZE( e ));
  25. printf("%zu\n", ASIZE( f ));
  26. printf("%zu\n", ASIZE( g ));
  27. printf("ASIZE_SAFE() only accepts arrays (try uncommenting).\n");
  28. printf("%zu\n", ASIZE_SAFE( a ));
  29. //printf("%zu\n", ASIZE_SAFE( b ));
  30. printf("%zu\n", ASIZE_SAFE( c ));
  31. //printf("%zu\n", ASIZE_SAFE( d ));
  32. printf("%zu\n", ASIZE_SAFE( e ));
  33. //printf("%zu\n", ASIZE_SAFE( f ));
  34. //printf("%zu\n", ASIZE_SAFE( g ));
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 2156KB
stdin
Standard input is empty
stdout
ASIZE() accepts pointers, producing invalid results.
3
2
2
1
5
4
1
ASIZE_SAFE() only accepts arrays (try uncommenting).
3
2
5