Skip to content

Instantly share code, notes, and snippets.

@zodiac1111
Last active August 29, 2015 13:56
Show Gist options
  • Save zodiac1111/9208141 to your computer and use it in GitHub Desktop.
Save zodiac1111/9208141 to your computer and use it in GitHub Desktop.
求数组元素个数
#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
int a[10];
int *n =a;
ARRAY_SIZE(a);
ARRAY_SIZE(n); // compile error :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment