Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 3 Oct 2000 23:27:49 +0200 (SAST)
From:      Robert Nordier <rnordier@nordier.com>
To:        Thomas David Rivers <rivers@dignus.com>
Cc:        dnelson@emsphone.com, lile@stdio.com, hackers@FreeBSD.ORG
Subject:   Re: Question about -Wchar-subscripts
Message-ID:  <200010032127.XAA29173@c2-dbn-97.dial-up.net>
In-Reply-To: <200010032013.QAA81703@lakes.dignus.com> "from Thomas David Rivers at Oct 3, 2000 04:13:14 pm"

next in thread | previous in thread | raw e-mail | index | archive | help
Thomas David Rivers wrote:

> > > So why is using a "char" as an array subscript wrong?  I had always
> > > avoided it because the compiler complained and that was good enough
> > > for me.
> > 
> > Because your char value could be negative and end up referencing memory
> > before your array start.  Mainly a problem with the ctype macros and
> > high-ascii characters.
> > 
> 
>  That's an interesting reason... any variable can be negative (well,
>  except for the unsigned types...)  - what's so interesting about
>  `char'?  Is it simply ctype macros that are the concern, or something
>  "bigger"?

What's interesting about char is that it's implementation defined
whether "plain" char is the equivalent of "signed char" or "unsigned
char" (or even something else).

So, given an 8-bit, two's complement implementation of char, the
statement

	char i = 128;

may cause 'i' to end up as -128 or 128, for example.

An implementation-defined value to your subscript is almost never
useful, so this kind of behavior does warrant a warning.  You'll
notice gcc doesn't warn if explicitly signed or unsigned chars are
used as subscripts, as then there is no uncertainty.

--
Robert Nordier

rnordier@nordier.com
rnordier@FreeBSD.org


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200010032127.XAA29173>