How to find size of an array in C without sizeof
61–70 of 212 posts
Re: How to find size of an array in C without sizeof
#62Was anyone else's first thought "Hmm... cool," followed by "I hope nobody asks me this on an interview?"
Re: How to find size of an array in C without sizeof
#63Earlier quoted context omitted.
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.
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
#64Earlier quoted context omitted.
Not likely, but possible. This reminds me of the bug that was found in the binary search algorithm a few years ago, IIRC, in Java. The interesting thing is that binary search is probably one of the earliest-invented algorithms. Yet, in the book Writing Efficient Programs by Jon Bentley (which I mentioned in a recent HN comment), he says that in a class he taught to several industrial programmers with many years of ex…
I think this is it: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6412541
Re: How to find size of an array in C without sizeof
#65Earlier quoted context omitted.
I'm not sure if it's a praise for C though. Arcane design and lack of clarity might be fun to decipher, but it's not something that you'd want to see in the programming language.
Doesn't every language turns into insanity to decipher once you look close enough?
Re: How to find size of an array in C without sizeof
#66The printf commands say "the address of..." but proceed to print out the value, not address.
Re: How to find size of an array in C without sizeof
#67The result you get with this trick is signed, while the result you get with sizeof is unsigned. Edit: Just to clarify, what you get is ptrdiff_t instead of size_t. So if array size is greater than PTRDIFF_MAX, you get undefined behavior [1]. [1] http://en.cppreference.com/w/c/types/ptrdiff_t
How likely do you run into array bigger than 2gb?
Re: How to find size of an array in C without sizeof
#68C is such a boondoggle of a language... We're condemned to forever explore its every weird nook and cranny for historical reasons, rather than because it is the cleanest, best approach to things possible.
C for sure has its weird sides, but does appear much more logical and consistent when observed "from the below", from how-the-hardware-runs perspective. For example, the shift operators have higher precedence than bitwise masking (and/or/xor) since this way the expressions setting/clearing ranges of bits won't require parentheses (so increased readability) and the masking constants in them will be the narrowest. Load…
Re: How to find size of an array in C without sizeof
#69Was anyone else's first thought "Hmm... cool," followed by "I hope nobody asks me this on an interview?"