Thread: warning: array subscript has type char [-Wchar-subscripts]

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    55

    warning: array subscript has type char [-Wchar-subscripts]

    I get this warning when trying to compile a code snippet from the net with GCC GNU compiler.

    Code:
    : warning: array subscript has type    char    [-Wchar-subscripts]
         arr[0] = (int)tolower(strMove[0]) - 'a';//letter here
         ^
    : warning: array subscript has type    char    [-Wchar-subscripts]
         arr[1] = (int)tolower(strMove[1]) - '0';
         ^
    : warning: array subscript has type    char    [-Wchar-subscripts]
         arr[2] = (int)tolower(strMove[2]) - 'a';//letter here
         ^
    : warning: array subscript has type    char    [-Wchar-subscripts]
         arr[3] = (int)tolower(strMove[3]) - '0';
         ^
    Compilation finished successfully.
    I roughly think it has something to do with signed and unsigned char conflict? I don't understand it though and have tried to fix it but without success.

    Code:
    int GetMove(int arr[]) {
    
        int count = 0;
        int i,j;
        int validInputArr[ROW_SIZE] = {0,1,2,3,4,5,6,7};
    
        String strMove;
        String strResign = "resign";
    
        printf("Enter coordinates in the form g4e4\n");
        printf("Enter resign to resign.\n");
    
        gets(strMove);
    
        if(StringCompare(strMove, strResign) == 1) {
            return RESIGN;
        } else if (strlen(strMove) != 4 ) {
            return INVALID;
        }
    
        //Convert the chars to ints, which gives ascii numbers. So then convert to
        //the decimal value by subtracting the base.
        arr[0] = (int)tolower(strMove[0]) - 'a';//letter here
        arr[1] = (int)tolower(strMove[1]) - '0';
        arr[2] = (int)tolower(strMove[2]) - 'a';//letter here
        arr[3] = (int)tolower(strMove[3]) - '0';
    
        for(i=0;i<4;i++) {
            for(j=0;j<ROW_SIZE;j++) {
                if(arr[i] == validInputArr[j]) {
                    count++;
                }
            }
        }
        if(count != 4) {
            return INVALID;
        }else {
            printf("Move piece from %i:%i to %i:%i\n", arr[0], arr[1], arr[2], arr[3]);
            return VALID;
        }
    
        return 1;
    }//End GetMove
    I am grateful for help to fix it,many thanks.

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    What's "String"?

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    OK-so I said this isn't my code but here is the relevant typedef:
    typedef char String[STRING_SIZE];
    Does that help?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It would be incredible for the compiler to think that 0 was a char constant. I might start looking to see whether you perhaps have a #define that has run amok -- maybe use the -E flag to get what's there after the preprocessor has gone through and look at those lines in the processed output.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    A little bit - it would be a lot more helpful if you could post a simplified yet complete, compilable program that produces the warning messages you've received (i.e. include a basic "main" and all necessary typedefs and includes).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Subscript requires array or pointer type."
    By nrr5094 in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2013, 06:08 PM
  2. warning: array subscript has type `char'
    By pc_doctor in forum C Programming
    Replies: 5
    Last Post: 12-08-2008, 03:03 PM
  3. program.c:50: warning: subscript has type `char'
    By AVandelay in forum C Programming
    Replies: 8
    Last Post: 09-22-2007, 04:02 PM
  4. "subscript requires array or pointer type"?
    By Nutka in forum C Programming
    Replies: 12
    Last Post: 12-06-2002, 05:51 PM
  5. Replies: 4
    Last Post: 11-09-2002, 01:16 PM