Re:How to find size of an array in C without sizeof
by Tempest1981 on Tuesday December 27, 2016 @03:20PM
(#13265891)
Great point, about proper macro defines.
And use do/while wrappers (without a trailing semicolon) where needed: https://kernelnewbies.org/FAQ/DoWhile0
While we're talking macros, anyone who reads the g-truc.net article should feel itchy after seeing the countof macro in their example:
Two problems here:1. The last use of 'arr' doesn't have 'arr' wrapped in parenthesis.
2. The entire expression is not wrapped in parentheses either.
If you write a macro that does any calculation like this, play it safe and put parens around every macro argument and parens around the entire expression too. Otherwise you never know what operator precedence will do to you.