I have a simple macro:
#define LENGTH(arr) (sizeof(arr)/sizeof(arr[0]))
and for whatever reason, it seems to work fine when setting something like:
int length = LENGTH(arr)
but not to compare with in a loop
while(i < LENGTH(arr))
For the same arr, the macro will either work or it won't.
The likely problem is that the arr in the loop is a pointer rather than an array. The macro does not work with pointers, it only works with arrays.
Remember that arrays decays to pointer when passed around. So if you pass an array to a function, it's not longer an array but a pointer inside the function. A pointer which have only have information about the type, but not the size of the array.
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