3 ms·
> #define countof(a) (sizeof(a) / sizeof(*(a))) > #define lengthof(s) (countof(s) - 1) It makes no sense to use the word "length" to mean one less than the
by da39a3ee 3y ago
> #define countof(a) (sizeof(a) / sizeof(*(a)))
> #define lengthof(s) (countof(s) - 1)
It makes no sense to use the word "length" to mean one less than the number of items. You could call it maxindexof perhaps.
There may be good arguments for zero based indexing, but we have to also accept that there are downsides. One is that your code has to feature an artificial quantity obtained by subtracting one from a meaningful quantity.
- abareplace 3y agoThis is length of a null-terminated string, e.g. lengthof("abc").
- da39a3ee 3y agoOK, thanks I don't use C much and definitely forgot about that. But my point still stands doesn't it, in that the author is using these preprocessor macros for general arrays, which don't have a special terminator sentinel.