"array subscript has type `char'" - what's wrong?

hi,
my gcc is complaining about this, but i dont undertand whats wrong about
having type char as subscript for array

chair.c: In function main': chair.c:816: warning: array subscript has typechar’
chair.c:827: warning: array subscript has type char' chair.c:828: warning: array subscript has typechar’
chair.c:960: warning: array subscript has type `char’

example source:
char sub;
int a[50];

sub=1;
a[sub]=5;

thanks for explanation
515

Hi,

This was not a C programming list last time I checked. Perhaps you
should find a programming list, or get a C book.

“char” is signed in gcc, (although this can not be relied on in other
compilers), put “unsigned char” and it works fine. (you could also put
"signed char", but leaving it unclear often drives gcc nuts).

Cheers

JG

on the 25/08/03 21:48, 515 wrote:> hi,

my gcc is complaining about this, but i dont undertand whats wrong about
having type char as subscript for array

chair.c: In function main': chair.c:816: warning: array subscript has typechar’
chair.c:827: warning: array subscript has type char' chair.c:828: warning: array subscript has typechar’
chair.c:960: warning: array subscript has type `char’

example source:
char sub;
int a[50];

sub=1;
a[sub]=5;

thanks for explanation
515

my gcc is complaining about this, but i dont undertand whats wrong
about having type char as subscript for array

The issue is that type char can be equivalent to either signed char or
unsigned char. The ISO C standard does not specify which, so each
compiler is free to choose whichever one it wants. So for example this
code:

char i = '\377';
n = array[i];

(Assume 8-bit chars.) Without knowing what compiler this code compiles
on, you can’t know ahead of time whether this will access array[255]
or array[-1].

To avoid this problem, you should explicitly use “unsigned char” (or
"signed char", depending on what you want) when declaring variables
that will be used as indexes. Alternately, you can provide an explicit
cast at the point that you use the value as an array index.

b

Hi,

This was not a C programming list last time I checked. Perhaps you
should find a programming list, or get a C book.

“char” is signed in gcc, (although this can not be relied on in other
compilers), put “unsigned char” and it works fine. (you could also put
"signed char", but leaving it unclear often drives gcc nuts).

The hack of casting it would probably work, too:

a[(int) sub] = 5;

-bill!

On Mon, Aug 25, 2003 at 10:11:22PM +0100, J. Grant wrote:

example source:
char sub;
int a[50];

sub=1;
a[sub]=5;


bill at newbreedsoftware.com “The patient has no previous
http://newbreedsoftware.com/bill history of suicides.”

gcc is complaining because it’s not valid C.

“A subscripting expression consists of a postfix expression, a left bracket,
an arbitrary expression, and a right bracket. This construction is used for
array subscripting, where the postfix expression (commonly an array name)
evaluates to a pointer to the beginning of the array and the other expression
to an integer offset.” (Emphasis mine.)

“C A Reference Manual”, Samuel P. Harbison, Guy L. Steele, Jr.,
Prentice-Hall, 1995, page 186. (This is a very common reference, and a good
thing to have on your bookshelf :slight_smile:

JOn Monday 25 August 2003 01:48 pm, 515 wrote:

hi,
my gcc is complaining about this,
chair.c:816: warning: array subscript has type `char’
char sub;
int a[50];
sub=1;
a[sub]=5;