Modern Electronics (was Re: List charter mods & headcount... ;

From: der Mouse <mouse_at_Rodents.Montreal.QC.CA>
Date: Wed Jun 23 20:15:36 2004

>> (if N were to be declared "unsigned", even more would get it wrong!)
> Really? Once you realize that C treats the value of 0 as false, and
> anything else as true, then there isn't any difference

Yes. But a lot of people will get confused because they know that --
wraps from zero to ~0U and won't read the test carefully enough.

> But if we're going to go into gotchas of C, what's wrong with this code?

[compressed vertically to save space -dM]
> void foo(char *s)
> { int charcount[256]; memset(charcount,0,sizeof(charcount));
> for ( ; *s ; s++) { charcount[*s]++; }
> }

Nothing...on machines where char is 8-bit unsigned. :-)

If char is signed, or if it's wider than 8 bits, you lose big - and
worse, you don't lose until it's called with a string that happens to
contain offending characters, so it may "work fine" in testing.

/~\ The ASCII der Mouse
\ / Ribbon Campaign
 X Against HTML mouse_at_rodents.montreal.qc.ca
/ \ Email! 7D C8 61 52 5D E7 2D 39 4E F1 31 3E E8 B3 27 4B
Received on Wed Jun 23 2004 - 20:15:36 BST

This archive was generated by hypermail 2.3.0 : Fri Oct 10 2014 - 23:37:00 BST