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

From: Sean 'Captain Napalm' Conner <spc_at_conman.org>
Date: Wed Jun 23 21:39:49 2004

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 ...)
Received on Wed Jun 23 2004 - 21:39:49 BST

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