char as array index gives warning in gcc

Discussion in 'C Programming' started by Eugene Rice, Oct 21, 2006.

  1. Eugene Rice

    Eugene Rice Guest

    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
    Eugene Rice, Oct 21, 2006
    #1
    1. Advertising

  2. Eugene Rice

    young Guest

    default is int
    young, Oct 21, 2006
    #2
    1. Advertising

  3. 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.
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Oct 21, 2006
    #3
  4. Eugene Rice

    Rg Guest

    On Oct 21, 1:13 pm, "Harald van Dijk" <> wrote:
    >
    > [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];
    }
    Rg, Oct 21, 2006
    #4
  5. Eugene Rice

    Malcolm Guest

    "Eugene Rice" <> wrote 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.
    Malcolm, Oct 21, 2006
    #5
  6. "young" <> writes:
    > 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) <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.
    Keith Thompson, Oct 21, 2006
    #6
  7. "Malcolm" <> writes:
    [...]
    > 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) <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.
    Keith Thompson, Oct 21, 2006
    #7
  8. Eugene Rice

    Jack Klein Guest

    On Sat, 21 Oct 2006 14:53:14 GMT, Eugene Rice <> wrote
    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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Oct 22, 2006
    #8
  9. Eugene Rice

    Malcolm Guest

    "Keith Thompson" <> wrote in message
    > "Malcolm" <> writes:
    > [...]
    >> 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.
    Malcolm, Oct 22, 2006
    #9
  10. Eugene Rice

    Eugene Rice Guest

    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
    Eugene Rice, Oct 22, 2006
    #10
  11. Eugene Rice

    Eugene Rice Guest

    Jack Klein wrote:
    > On Sat, 21 Oct 2006 14:53:14 GMT, Eugene Rice <> wrote
    > 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
    Eugene Rice, Oct 22, 2006
    #11
  12. Eugene Rice

    Eric Sosman Guest

    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
    lid
    Eric Sosman, Oct 22, 2006
    #12
  13. Eugene Rice <> writes:
    [...]
    > 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) <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.
    Keith Thompson, Oct 22, 2006
    #13
  14. On Sat, 21 Oct 2006 20:03:17 GMT, Keith Thompson <>
    wrote:

    > "Malcolm" <> writes:
    > [...]
    > > 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
    Dave Thompson, Nov 6, 2006
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lovecreatesbeauty
    Replies:
    1
    Views:
    1,044
    Ian Collins
    May 9, 2006
  2. Replies:
    3
    Views:
    732
  3. davidb
    Replies:
    0
    Views:
    761
    davidb
    Sep 1, 2006
  4. Replies:
    5
    Views:
    462
    Daniel T.
    Jul 8, 2007
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    284
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page