From owner-freebsd-hackers Tue Oct 3 21:10: 6 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from smtp2.vnet.net (smtp2.vnet.net [166.82.1.32]) by hub.freebsd.org (Postfix) with ESMTP id 940BF37B502 for ; Tue, 3 Oct 2000 21:10:00 -0700 (PDT) Received: from dignus.com (ponds.vnet.net [166.82.177.48]) by smtp2.vnet.net (8.10.1/8.10.1) with ESMTP id e9449qT12284; Wed, 4 Oct 2000 00:09:54 -0400 (EDT) Received: from lakes.dignus.com (lakes.dignus.com [10.0.0.3]) by dignus.com (8.9.2/8.8.5) with ESMTP id AAA41639; Wed, 4 Oct 2000 00:09:50 -0400 (EDT) Received: (from rivers@localhost) by lakes.dignus.com (8.9.3/8.6.9) id AAA83630; Wed, 4 Oct 2000 00:09:50 -0400 (EDT) Date: Wed, 4 Oct 2000 00:09:50 -0400 (EDT) From: Thomas David Rivers Message-Id: <200010040409.AAA83630@lakes.dignus.com> To: rivers@dignus.com, rnordier@nordier.com Subject: Re: Question about -Wchar-subscripts Cc: dnelson@emsphone.com, hackers@FreeBSD.ORG, lile@stdio.com In-Reply-To: <200010032127.XAA29173@c2-dbn-97.dial-up.net> Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Robert Nordier wrote: > > 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 Ah - yes! That makes perfect sense... when you consider that `char' all alone can be signed or unsigned... Thanks for the explanation! - Dave Rivers - To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message