Re:How to find size of an array in C without sizeof
by gpderetta on Monday December 26, 2016 @04:05PM
(#13259767)
I doubt the author meant for this trick to be actually used, they were just showing how pointers to arrays are typed correctly in a clever way.
[ Parent
| on HN ]
Re:How to find size of an array in C without sizeof
by Stratoscope on Monday December 26, 2016 @04:11PM
(#13259787)
Indeed, one could hope that is the case! :-)
But my point with suggesting the macro applies equally to the more traditional sizeof division. I have seen code that divides the two sizeofs every time an array length is needed. I think it's better to put that calculation in a macro so you only do it in one place.
Re:How to find size of an array in C without sizeof
by markrages on Monday December 26, 2016 @05:21PM
(#13260085)
You are dividing one constant by another -- surely that would be handled at compile time?
Re:How to find size of an array in C without sizeof
by Stratoscope on Monday December 26, 2016 @05:54PM
(#13260211)
You are correct, the compiled code will be the same whether you use a macro or not. In fact, this is true for any C macro. A macro is merely a source code text substitution done by the preprocessor. Using a macro is exactly the same as writing out the equivalent macro expansion everywhere you use it.
My suggestion to use a macro is not because of any difference in the compiled code, but to improve the readability of the source code.