Standard alternative to sizeof(array)/sizeof(array[0])?
Is there a good standard alternative to the
Code:
sizeof(array)/sizeof(array[0])
used for determining the size of an array?
Well sometimes you see:
Code:
sizeof array/sizeof*array
but this is only somewhat shorter and not hardly better.
What I'm looking for is a standard macro in a standard header, something like
Code:
#define lenof(a) (sizeof(a)/sizeof((a)[0]))
but there is no such thing in stddef.h where offsetof is defined.
There is the further problem, that
Code:
int foo(short array[8])
{
return lenof(array);
}
will return 2 not 8 on most targets, due to the fact that foo is equal to
Code:
int foo(short *array)
which is a rather obscure feature of C.