Defining a Macro to Return the Number of Elements in an Array
As a convenience I attempted to define this macro for providing the number of elements in a static array of an arbitrary type:
#define ARSIZE(AR) (sizeof(AR)/sizeof(*AR))
This did not work because the size of operator cannot be directly used in a macro. I have seen here:
http://cs-fundamentals.com/tech-interview/c/implement-sizeof-operator-in-c.php
how to create a macro that would work for the numerator of the above #define:
#define NUMSIZE(AR) (char *)(&AR+1) - (char *)(&AR)
But what about the denominator which needs to be the size of the type? Would this work?
#define DENOMSIZE(AR) (char *)(&AR[1]) - (char *)(&AR[0])
If it did work would it still work for a single element array?
Would the above method be more expensive in processing time than giving up on the use of a macro to do this and expanding to the equivalent of (sizeof(AR)/sizeof(*AR) manually?