Secure countof()

Run Settings
LanguageC
Language Version
Run Command
#include <stdio.h> #include <assert.h> #define countof(a) (assert((void *)(a) == (void *)&(a)), sizeof(a) / sizeof(*(a))) int main(void) { int a[20] = {0}; int *b = a; int count_a = countof(a); //int count_b = countof(b); // fails // Passing a function will give incorrect answer. But I find that to be an unlikely mistake. int count_main = countof(main); printf("Array count: %i\n", count_a); printf("Function: %i // failure.\n", count_main); return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines