473,390 Members | 1,301 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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 7666
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: J. Campbell | last post by:
I'm a novice with c/c++ and have been reading Eckel's book. I'd like some feedback on using this method. What I need to do is treat a string as numeric data. I know how to write functions to...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
16
by: Dave | last post by:
Hi all, I have a 4 byte char array with the binary data for two 16-bit signed integers in it like this: Index 3 2 1 0 Data Bh Bl Ah Al Where Bh is the high byte of signed 16-bit...
9
by: Angus | last post by:
Hello I want to write a function which returns a char array. But if I write this: char myfunction() { return "123"; }
4
by: =?Utf-8?B?cm9nZXJfMjc=?= | last post by:
hey, I have a method that takes a char array of 10. I have a char array of 30. how do I make it send the first 10, then the next 10, then the final 10 ? I need help with my looping skills....
8
by: Frank Liebelt | last post by:
Hi I try to convert a int array into a char array. My code: void exec() { char mds; int i; int mdc =...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
4
by: Rizladonovich | last post by:
I am currently reading/learning c. I often like to look at code while I read, - to see more practical use then often are feasible in educational books. I am looking at some code from wget. Code...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.