What does this line exactly mean?
It is clear what define is, but I don't understand why is passing the pointer of x at the denominator:
#define ArrayLength(x) (sizeof(x)/sizeof(*(x)))
thanks
sizeof(*(x))
is the length of the first element in the array in bytes. The variable x
is of an array type, and it decays to a pointer, pointing to the start of the array. The asterisk (*
) is the dereference operator, so *(x)
means "the data pointed to by x
".
sizeof(x)
applies the sizeof
operator to an array type. This gives the length of the entire array in bytes.
#define ArrayLength(x) (sizeof(x)/sizeof(x[0]))
which is perhaps easier to read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With