473,473 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 7676
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
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.