Last active
August 29, 2015 13:56
-
-
Save zodiac1111/9208141 to your computer and use it in GitHub Desktop.
求数组元素个数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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