468,491 Members | 2,042 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

char as array index gives warning in gcc

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

Oct 21 '06 #1
13 7081
default is int

Oct 21 '06 #2
Eugene Rice wrote:
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.

Oct 21 '06 #3
Rg
On Oct 21, 1:13 pm, "Harald van Dijk" <true...@gmail.comwrote:
>
[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];
}

Oct 21 '06 #4

"Eugene Rice" <ge**@verizon.netwrote in message
news:uvq_g.1146$Wp3.304@trndny05...
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.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.
Oct 21 '06 #5
"young" <yo***********@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 21 '06 #6
"Malcolm" <re*******@btinternet.comwrites:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 21 '06 #7
On Sat, 21 Oct 2006 14:53:14 GMT, Eugene Rice <ge**@verizon.netwrote
in comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 22 '06 #8
"Keith Thompson" <ks***@mib.orgwrote in message
"Malcolm" <re*******@btinternet.comwrites:
[...]
>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.
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.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.

Oct 22 '06 #9
Harald van Dijk wrote:
Eugene Rice wrote:
>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
Oct 22 '06 #10
Jack Klein wrote:
On Sat, 21 Oct 2006 14:53:14 GMT, Eugene Rice <ge**@verizon.netwrote
in comp.lang.c:
>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
Oct 22 '06 #11
Eugene Rice wrote:
Harald van Dijk wrote:
>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>

--
Eric Sosman
es*****@acm-dot-org.invalid
Oct 22 '06 #12
Eugene Rice <ge**@verizon.netwrites:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 22 '06 #13
On Sat, 21 Oct 2006 20:03:17 GMT, Keith Thompson <ks***@mib.org>
wrote:
"Malcolm" <re*******@btinternet.comwrites:
[...]
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.
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
Nov 6 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by J. Campbell | last post: by
29 posts views Thread by shmartonak | last post: by
9 posts views Thread by Angus | last post: by
4 posts views Thread by =?Utf-8?B?cm9nZXJfMjc=?= | last post: by
8 posts views Thread by Frank Liebelt | last post: by
5 posts views Thread by =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.