From owner-freebsd-hackers Tue Oct 3 14:29:38 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from c2-dbn-97.dial-up.net (c2-dbn-97.dial-up.net [196.34.155.225]) by hub.freebsd.org (Postfix) with ESMTP id 579DE37B503 for ; Tue, 3 Oct 2000 14:29:31 -0700 (PDT) Received: (from rnordier@localhost) by c2-dbn-97.dial-up.net (8.8.7/8.6.12) id XAA29173; Tue, 3 Oct 2000 23:27:52 +0200 (SAST) From: Robert Nordier Message-Id: <200010032127.XAA29173@c2-dbn-97.dial-up.net> Subject: Re: Question about -Wchar-subscripts In-Reply-To: <200010032013.QAA81703@lakes.dignus.com> "from Thomas David Rivers at Oct 3, 2000 04:13:14 pm" To: Thomas David Rivers Date: Tue, 3 Oct 2000 23:27:49 +0200 (SAST) Cc: dnelson@emsphone.com, lile@stdio.com, hackers@FreeBSD.ORG X-Mailer: ELM [version 2.4ME+ PL82 (25)] MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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