char as array index gives warning in gcc

E

Eugene Rice

I'm writing C code for an Atmel AVR micro controller. Because RAM space is extremely limited (about 500 bytes) I use char variables wherever I can. For example I use chars as array indices in dozens of places:

char arr[3];
char x,y,z;

for (x=0; x<3; x++) {
arr[x] = ...; // gcc gives warning here
// more code
}

The gcc AVR compiler I use generates a warning for each char array index as above, leading to warning message clutter on the compiler output. Normally I like clean compiles, but I think that my char index usage is neither illegal nor indicative of an error.

What do people think? Is the gcc warning in any way useful in diagnosing a possible error? Am I right that my usage is completely legal?

I might be able to quiet down gcc by putting in int casts, but that would clutter the code, and to me casts say to the reader "yes I know I'm doing something questionable, but trust me I've analyzed the situation and the code is correct", yet as I said I don't think my char indices are questionable.

Thanks,

Geno
 
G

Guest

Eugene said:
I'm writing C code for an Atmel AVR micro controller. Because RAM space is extremely limited (about 500 bytes) I use char variables wherever I can. For example I use chars as array indices in dozens of places:

char arr[3];
char x,y,z;

for (x=0; x<3; x++) {
arr[x] = ...; // gcc gives warning here
// more code
}

The gcc AVR compiler I use generates a warning for each char array index as above, leading to warning message clutter on the compiler output. Normally I like clean compiles, but I think that my char index usage is neither illegal nor indicative of an error.

What do people think? Is the gcc warning in any way useful in diagnosing a possible error? Am I right that my usage is completely legal?

Yes, and yes (in your example).
I might be able to quiet down gcc by putting in int casts, but that would clutter the code, and to me casts say to the reader "yes I know I'm doing something questionable, but trust me I've analyzed the situation and the code is correct", yet as I said I don't think my char indices are questionable.

In the general case, char indexes are problematic because they may be
either signed or unsigned, depending on the implementation. If a
user-provided character is used as an array index, it's possible that
the value is negative, and in most cases, that would mean memory
outside of the array will be accessed. It was apparently a common
enough problem to warn about.

If you do not want GCC to complain about this, either use the correct
command-line option to disable this warning (check the documentation),
or declare your variables to be of type signed char or unsigned char,
depending on your needs.
 
R

Rg

[snip]

If you do not want GCC to complain about this, either use the correct
command-line option to disable this warning (check the documentation),
or declare your variables to be of type signed char or unsigned char,
depending on your needs.

But if you still need signed char and you still want to index array
with them, just typecast them to unsigned char.

int main(void)
{
int v[] = { 1, 2, 3 };
char c = 0;

return v[(unsigned char)c];
}
 
M

Malcolm

Eugene Rice said:
I'm writing C code for an Atmel AVR micro controller. Because RAM space
is extremely limited (about 500 bytes) I use char variables wherever I
can. For example I use chars as array indices in dozens of places:

char arr[3];
char x,y,z;

for (x=0; x<3; x++) {
arr[x] = ...; // gcc gives warning here
// more code
}

The gcc AVR compiler I use generates a warning for each char array index
as above, leading to warning message clutter on the compiler output.
Normally I like clean compiles, but I think that my char index usage is
neither illegal nor indicative of an error.
What do people think? Is the gcc warning in any way useful in diagnosing
a possible error? Am I right that my usage is completely legal?

I might be able to quiet down gcc by putting in int casts, but that would
clutter the code, and to me casts say to the reader "yes I know I'm doing
something questionable, but trust me I've analyzed the situation and the
code is correct", yet as I said I don't think my char indices are
questionable.
Indexing with a character is one of those dubious things.
OK in ASCII, asking for trouble if someone tries to use another character
set with negative or very large positive values.
You seem to be using char as a tiny integer, in which case it should be
signed / unsigned char explicitly to tell reader that the variable is not a
character.
 
K

Keith Thompson

young said:
default is int

What default is int?

Please quote context when you post a followup; otherwise we can't tell
what you're talking about.

There is no default type for array indices; the index merely has to be
of some integer type.
 
K

Keith Thompson

Malcolm said:
Indexing with a character is one of those dubious things.
OK in ASCII, asking for trouble if someone tries to use another character
set with negative or very large positive values.
You seem to be using char as a tiny integer, in which case it should be
signed / unsigned char explicitly to tell reader that the variable is not a
character.

I don't see what ASCII has to do with it. Plain char is guaranteed to
be able to represent values from 0 to 127, regardless of the character
set.

But yes, if you're using a char type as a small integer, you should
use signed char or unsigned char explicitly.
 
J

Jack Klein

I'm writing C code for an Atmel AVR micro controller. Because RAM space is extremely limited (about 500 bytes) I use char variables wherever I can. For example I use chars as array indices in dozens of places:

char arr[3];
char x,y,z;

for (x=0; x<3; x++) {
arr[x] = ...; // gcc gives warning here
// more code
}

The gcc AVR compiler I use generates a warning for each char array index as above, leading to warning message clutter on the compiler output. Normally I like clean compiles, but I think that my char index usage is neither illegal nor indicative of an error.

What do people think? Is the gcc warning in any way useful in diagnosing a possible error? Am I right that my usage is completely legal?

I might be able to quiet down gcc by putting in int casts, but that would clutter the code, and to me casts say to the reader "yes I know I'm doing something questionable, but trust me I've analyzed the situation and the code is correct", yet as I said I don't think my char indices are questionable.

Now you know the downside of posting to moderated groups. Here is the
reply I sent to comp.lang.c.moderated a week ago:

The code is legal if and only if one of the two following conditions
are met:

1. The type char is unsigned on your implementation.

2. The type char is signed, but you never use a char with a negative
value for an index into the array.

The simplest thing might be to change the type of 'x' in the
definition from char to unsigned char, and see if that eliminates the
warning.
 
M

Malcolm

Keith Thompson said:
I don't see what ASCII has to do with it. Plain char is guaranteed to
be able to represent values from 0 to 127, regardless of the character
set.

But yes, if you're using a char type as a small integer, you should
use signed char or unsigned char explicitly.
This is often handy

void *lookup[1 << CHAR_BIT];

/* some code */

char *str;
void *usefulptr;

lookup[*str] = usefulptr;

Great for ASCII. Will gobble too much memory with a big character set, and
break if negative chars are allowed.
 
E

Eugene Rice

Harald said:
Eugene said:
I'm writing C code for an Atmel AVR micro controller. Because RAM space is extremely limited (about 500 bytes) I use char variables wherever I can. For example I use chars as array indices in dozens of places:

char arr[3];
char x,y,z;

for (x=0; x<3; x++) {
arr[x] = ...; // gcc gives warning here
// more code
}

The gcc AVR compiler I use generates a warning for each char array index as above, leading to warning message clutter on the compiler output. Normally I like clean compiles, but I think that my char index usage is neither illegal nor indicative of an error.

What do people think? Is the gcc warning in any way useful in diagnosing a possible error? Am I right that my usage is completely legal?

Yes, and yes (in your example).
I might be able to quiet down gcc by putting in int casts, but that would clutter the code, and to me casts say to the reader "yes I know I'm doing something questionable, but trust me I've analyzed the situation and the code is correct", yet as I said I don't think my char indices are questionable.

In the general case, char indexes are problematic because they may be
either signed or unsigned, depending on the implementation. If a
user-provided character is used as an array index, it's possible that
the value is negative, and in most cases, that would mean memory
outside of the array will be accessed. It was apparently a common
enough problem to warn about.

If you do not want GCC to complain about this, either use the correct
command-line option to disable this warning (check the documentation),
or declare your variables to be of type signed char or unsigned char,
depending on your needs.

[op]

I did check the gcc documentation and there was no command line option to suppress this warning. I would be happy to be proved wrong.

Geno
 
E

Eugene Rice

Jack said:
I'm writing C code for an Atmel AVR micro controller. Because RAM space is extremely limited (about 500 bytes) I use char variables wherever I can. For example I use chars as array indices in dozens of places:

char arr[3];
char x,y,z;

for (x=0; x<3; x++) {
arr[x] = ...; // gcc gives warning here
// more code
}

The gcc AVR compiler I use generates a warning for each char array index as above, leading to warning message clutter on the compiler output. Normally I like clean compiles, but I think that my char index usage is neither illegal nor indicative of an error.

What do people think? Is the gcc warning in any way useful in diagnosing a possible error? Am I right that my usage is completely legal?

I might be able to quiet down gcc by putting in int casts, but that would clutter the code, and to me casts say to the reader "yes I know I'm doing something questionable, but trust me I've analyzed the situation and the code is correct", yet as I said I don't think my char indices are questionable.

Now you know the downside of posting to moderated groups. Here is the
reply I sent to comp.lang.c.moderated a week ago:

The code is legal if and only if one of the two following conditions
are met:

1. The type char is unsigned on your implementation.

2. The type char is signed, but you never use a char with a negative
value for an index into the array.

The simplest thing might be to change the type of 'x' in the
definition from char to unsigned char, and see if that eliminates the
warning.

[op]

Thanks, I will definitely try defining my char indices as unsigned char. I will report back to the group.

Geno
 
E

Eric Sosman

Eugene said:
Harald said:
Eugene Rice wrote:
[...]
The gcc AVR compiler I use generates a warning for each char array
index as above, leading to warning message clutter on the compiler
output. [...]

I did check the gcc documentation and there was no command line option
to suppress this warning. I would be happy to be proved wrong.

<proof topicality=minimal>

Not even "-Wno-char-subscripts"?

</proof>
 
K

Keith Thompson

Eugene Rice said:
Thanks, I will definitely try defining my char indices as unsigned
char. I will report back to the group.

When you do, please limit your lines to 72 columns or less. Not all
newsreaders deal well with very long lines.
 
D

Dave Thompson

I don't see what ASCII has to do with it. Plain char is guaranteed to
be able to represent values from 0 to 127, regardless of the character
set.
ASCII is exactly codes 0 to 127. On a system with 8-bit byte and an
ASCII-based codeset, as most are nowadays, the 'extended' codes, or
the ones that are e.g. 8859 but not ASCII/646, are exactly the ones
that might cause problems in a signed char or plain=signed char.
But yes, if you're using a char type as a small integer, you should
use signed char or unsigned char explicitly.

Concur.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top