Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > using character as array subscript

Reply
Thread Tools

using character as array subscript

 
 
Mirco Wahab
Guest
Posts: n/a
 
      06-18-2008
James Kanze wrote:
> g++ 4.1.0 (under Solaris) definitely warns in this case when
> -Wall -pedantic is used.
> ...
> So does 3.4.0 under Solaris, and the CygWin version of 3.4.4
> under Windows.
> I have no problem reproducing his warnings, with several
> different versions of g++, as long as -Wall is used. The actual
> warning is "char-subscripts", so adding -Wno-char-subscripts
> *after* -Wall (or not using -Wall at all, but choosing
> explicitly for each warning) will suppress it. Which you
> probably should do---this is one of those brain dead warnings of
> which every compiler seems to have a few.


OK, I checked again (-Wall, -pedantic if possible):

1) gcc version 3.4.2 (mingw-special)
/s/misc/charsubscr/charsubscr.cxx:6: warning: array subscript has type `char'
/s/misc/charsubscr/charsubscr.cxx:7: warning: array subscript has type `char'

2) gcc version 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
charsubscr.cxx:6: warning: array subscript has type `char'
charsubscr.cxx:7: warning: array subscript has type `char'

3) gcc version 4.2.3 20071030 (Linux)
(no warning)

4) gcc version 4.3.1 20080507 [gcc-4_3-branch revision 135036] (Linux)
(no warning)

5) icpc Version 10.1 (Linux)
(no warning)

6) Visual C++ 6 (SP6), Warning Level 4 (XP/SP2)
(no warning)

7) Visual C++ 9 (SP0), Warning Level 4 (XP/SP2)
(no warning)


So the gcc < 4.x seems to be the only tool
that emits this warning (?).

Thanks & Regards

Mirco
 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      06-18-2008
In article <d55ff98f-755c-4a85-86f5-8981ab3afe48
@f36g2000hsa.googlegroups.com>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...
> On Jun 17, 7:27 pm, Jerry Coffin <(E-Mail Removed)> wrote:
> > In article <4857ed5a$0$12713$(E-Mail Removed)>,
> > (E-Mail Removed) says...

>
> > [ ... ]

>
> > > Right, but using 'a' as an index into an array could be a
> > > different index on different compilers. considering that
> > > char could be signed and negative, you could have serious
> > > consequences.

>
> > > Granted, this isn't a problem in practice, but its not
> > > portable that foo['a'] = 1 should do something specific.

>
> > That depends on what you mean by something specific.
> > Basically, the behavior is unspecified, but NOT undefined.

>
> The behavior is exactly specified (or at least, as specified as
> anything else in C++). You index the array with the value
> corresponding to the encoding of a small a in the native
> character encoding. If the goal is to index the entry
> corresponding to the encoding of a small a, this is the only
> correct and specified way of doing it.


Right -- all I meant is that the order in which most of those entries
are arranged isn't specified. IIRC, the only part that's specified is
that the digits will be in order and contiguous.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      06-18-2008
On Jun 18, 11:01 am, Mirco Wahab <(E-Mail Removed)-halle.de> wrote:
> James Kanze wrote:
> > g++ 4.1.0 (under Solaris) definitely warns in this case when
> > -Wall -pedantic is used.
> > ...
> > So does 3.4.0 under Solaris, and the CygWin version of 3.4.4
> > under Windows.
> > I have no problem reproducing his warnings, with several
> > different versions of g++, as long as -Wall is used. The actual
> > warning is "char-subscripts", so adding -Wno-char-subscripts
> > *after* -Wall (or not using -Wall at all, but choosing
> > explicitly for each warning) will suppress it. Which you
> > probably should do---this is one of those brain dead warnings of
> > which every compiler seems to have a few.


> OK, I checked again (-Wall, -pedantic if possible):


[...]
> So the gcc < 4.x seems to be the only tool that emits this
> warning (?).


I get it with g++ 4.1. So maybe they realized how stupid it
was, and got rid of it (or at least dropped it from -Wall).

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      06-19-2008
Jerry Coffin <(E-Mail Removed)> writes:

> In article <d55ff98f-755c-4a85-86f5-8981ab3afe48
> @f36g2000hsa.googlegroups.com>, (E-Mail Removed) says...
>> On Jun 17, 7:27 pm, Jerry Coffin <(E-Mail Removed)> wrote:
>> > In article <4857ed5a$0$12713$(E-Mail Removed)>,
>> > (E-Mail Removed) says...

>>
>> > [ ... ]

>>
>> > > Right, but using 'a' as an index into an array could be a
>> > > different index on different compilers. considering that
>> > > char could be signed and negative, you could have serious
>> > > consequences.

>>
>> > > Granted, this isn't a problem in practice, but its not
>> > > portable that foo['a'] = 1 should do something specific.

>>
>> > That depends on what you mean by something specific.
>> > Basically, the behavior is unspecified, but NOT undefined.

>>
>> The behavior is exactly specified (or at least, as specified as
>> anything else in C++). You index the array with the value
>> corresponding to the encoding of a small a in the native
>> character encoding. If the goal is to index the entry
>> corresponding to the encoding of a small a, this is the only
>> correct and specified way of doing it.

>
> Right -- all I meant is that the order in which most of those entries
> are arranged isn't specified. IIRC, the only part that's specified is
> that the digits will be in order and contiguous.


The order is the least of the problems we have with a['a']. The main
problem is that 'a' is of type char, and char is often signed char,
therefore 'a' might be negative 0, and 'à' will most probably be
negative.

So you can use bytes to index arrays, but be careful:

int a[UCHAR_MAX+1];

char i=42;
if(0<=i){
a[i]; // ok
}

char j='a';
a[(unsigned char)j]; // ok

unsigned char k='a';
a[k]; // best

--
__Pascal Bourguignon__
 
Reply With Quote
 
thomas.mertes@gmx.at
Guest
Posts: n/a
 
      06-19-2008
On 17 Jun., 00:48, Ivan <(E-Mail Removed)> wrote:
> Hi,
>
> What is the best syntax to use a char to index into an array.
>
> ///////////////////////////////////
> For example
>
> int data[256];
>
> data['a'] = 1;
> data['b'] = 1;
> ///////////////////////////////////
>
> gcc is complaining about this syntax, so i am using static cast on the
> character literal. Is there a better way to do this?


It would be helpful, to post also the gcc warnings (complaints).

Greetings Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      06-19-2008
In article <(E-Mail Removed)>,
(E-Mail Removed) says...

[ ... ]

> The order is the least of the problems we have with a['a']. The main
> problem is that 'a' is of type char, and char is often signed char,
> therefore 'a' might be negative 0, and 'Ã*' will most probably be
> negative.


The standard specifically requires that all members of the basic
execution character set be nonnegative and 'a' is a member of the basic
execution character set, so it will never be negative.

ONLY characters that are NOT members of the basic execution character
set can be encoded with negative values. That includes a lot, but there
ARE limits.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      06-20-2008
Pete Becker <(E-Mail Removed)> writes:

> On 2008-06-19 07:02:05 -0400, Pete Becker <(E-Mail Removed)> said:
>
>> On 2008-06-19 05:18:46 -0400, (E-Mail Removed) (Pascal
>> J. Bourguignon) said:
>>
>>> The order is the least of the problems we have with a['a']. The
>>> main
>>> problem is that 'a' is of type char, and char is often signed char,
>>> therefore 'a' might be negative 0, and 'à' will most probably be
>>> negative.
>>>

>> Phew, I knew it had to be there somewhere, and I just found it:
>> [lex.charset]/3: "For each basic execution character set, the
>> values of the members shall be non-negative and distinct from one another."
>> So, in particular, 'a' cannot be negative.

>
> Sorry, missed the accent over that last 'a'. That character is not in
> the basic execution character set, so its value can be negative.


Yes, but thanks for the reference, at least 'a' is not negative.

--
__Pascal Bourguignon__
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to overload subscript of 2D-array DaVinci C++ 5 05-10-2006 10:31 PM
array subscript type cannot be `char`? Pedro Graca C Programming 51 03-28-2006 10:40 PM
out of range array subscript Richard Delorme C Programming 5 05-15-2004 03:42 PM
Order of uknown array subscript Tom Page C++ 4 02-17-2004 02:55 PM
Using undef as an array subscript Yehuda Berlinger Perl Misc 8 07-01-2003 07:04 PM



Advertisments