Something I thought I'd share, since I didn't realize why it had been done this way until doing research into Remcon's BV posts. Take this code example:
int get_rflag( char *flag )
unsigned int x;
for( x = 0; x < ( sizeof( r_flags ) / sizeof( r_flags ) ); x++ )
if( !str_cmp( flag, r_flags[x] ) )
I always wondered - why did they use sizeof in this manner? I never really understood it, but accepted it because it "just worked". Well after reading http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/expre_25.asp
I finally get it.
The r_flags array is a group of text strings for room flags. This function goes through the array and matches up the argument against that array and returns a result based on that. So how did it know how many elements there were?
The sizeof(r_flags) part knows. That operation returns the number of elements in the array. The value it gets back is multiplied by the size of the datatypes used by the array, in this case char*, which are typically 4. It then needs to be divided by the sizeof(r_flags), which is the size of the char* type, typically 4. Once it divides this out, it knows how many times to go over this loop.
All in all, it's actually quite clever and means the text arrays don't need a fixed size value. Adding an element is easy, and the get_rflag function doesn't need to know about it. I of course realize this may have been plainly obvious to some of you, so this post will hopefully explain it for those who were like me and didn't realize what was going on.