The only time I've found it useful is making macros that expect an array a little safer in C++. For example:
#define count(array) (sizeof(array) / sizeof(0[array]))
...will conveniently cause a compile error if you accidentally pass in a std::vector instead of an array.Thanks for the heads up. Not suprisingly, a related stackoverflow q&a mentions that macro and also another more convoluted one from Chromium that protects against accidental pointers being passed: http://stackoverflow.com/a/1598827
I have been C programming on and off for 20 years, and thought I know every trick. But no.
Re:Having fun abusing the C language
by beeforpork on Tuesday October 25, 2016 @04:33PM
(#12791412)
20 years it is for me, too. And the exact rules of integer constants and their exact type have puzzled me lately. So I was not surprised to find more tricks for countof() here, to make it more safe to use. Nice!
Also, typechecking macro arguments with ?: is quite interesting, check it out. :-)
Re:Having fun abusing the C language
by adamnemecek on Tuesday October 25, 2016 @12:03PM
(#12789037)
> but it was useless knowledge.
welcome to C programming!
It was over 25 years ago that I first came across the C language FAQ from the Usenet group comp.lang.c and it explained the "commutative property" of array subscripts. (Online version here: http://c-faq.com/aryptr/joke.html). Another similar stackoverflow Q&A has also been done: http://stackoverflow.com/questions/381542/with-c-arrays-why-...
Over the last 2 decades, I have never needed the knowledge that the C language lets you put a number outside the brackets instead of inside it. Other than deciphering obfuscated C puzzles, I've never run across any real world source code that took advantage of that syntactic quirk. So yes, I did learn that trivia about array subscripts -- but it was useless knowledge.