It was thus said that the Great der Mouse once stated:
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. :-)
Technically, the sign of a char is compiler dependent, not system
dependent. It wouldn't surprise me at all if say, the IRIX compiler treated
unspecified chars as signed, and GCC unsigned (I don't recall anymore the
details under IRIX---it's been too many years).
The code would be better written as:
#include <limits.h>
void foo(char *s)
{
size_t charcount[-CHAR_MIN + CHAR_MAX];
memset(charcount,0,sizeof(charcount));
for ( ; *s ; s++)
charcount[*s + -CHAR_MIN]++;
}
-spc (Who probably uses limits.h more than other programmers he knows ...)