Thread: warning: array subscript has type `char'

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    20

    warning: array subscript has type `char'

    Hi,

    Im getting these warnings when gccing with -Wall

    sample-.c: In function `ISls':
    sample-.c:214: warning: array subscript has type `char'
    sample-.c:218: warning: array subscript has type `char'
    sample-.c:227: warning: array subscript has type `char'
    sample-.c:229: warning: array subscript has type `char'
    sample-.c:236: warning: array subscript has type `char'
    sample-.c:255: warning: char format, DIR arg (arg 2)

    I've marked the lines in the lode in the left hand side with a /**/

    Just give me a pointer to my mistake.

    Cheers.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Here,
    Code:
    if( sort[i] != '\0')
    you are comparing sort[i] (an integer) with a character. Pick one datatype and stick with it.

    Here,
    Code:
    if(get_info.st_mode & S_IFDIR) printf("%c[94;1m%s%c[0m", 27, sort [i], 27);
    sort[i] is still an int, but you are calling it a string.

    Your other errors are similar.

    EDIT: Well, there exists the possibility that variable i has been defined as a char - but I can't tell for sure, since you must have declared it as a global. If you did declare i as a char, shame on you.
    Last edited by Dino; 12-08-2008 at 01:10 PM. Reason: I had an additional thought...
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Here is the warning
    Code:
    char *sort[100], i;
    i should be integer. If it's not the it is probably "casted"/translated to an int. Why you ask? Because inside the [] of the array pointer arithmetic is done. For example
    Code:
    char array[10];
    int five = 5;
    arr[five ];
    The compiler will add five to the value of arr, thus *(arr+five ) in order to determine the element. Since arr is a pointer, thus the size of the int (always?) it needs an int.

    Just cast it, like arr[(int)i] if you want it a char or ignore the warning, but it is better to cast

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    AHAA!! Thanks C_ntua - I missed that declaration. Yuk - multiple variable declarations on a single line... shame on you again.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is best to avoid casts. And by that I mean design code that avoids casts in the first place, because casts usually shuts the compiler up, regardless if it's right or wrong.
    So use the compiler to your help - if it warns or complains, think of another solution first, and if you know it is correct, be sure to be very sure, before you add a cast to silence a warning or error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think you must have tried to compile something that has already been compiled into something.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  4. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM