sir psycho sexy
sir psycho sexy

Reputation: 800

Array subscript has type 'char' warning is showed without using arrays

Hi I got this weird warning in this simple code that leaves me completely confused. I searched trough the page and I see that this is a warning to alert users to avoid using chars as index of matrix because they can be signned, but obviously this is not the case.

Here is the code:

#include <stdio.h>
#include <ctype.h>

int main() {

    char c='t';
    if (isspace(c)==0)
        printf ("%c is not a space",c);

    return (EXIT_SUCCESS);
}

my question is what is the reason of the warning? is it related with the fact that isspace expects an int as argument?

Upvotes: 0

Views: 423

Answers (1)

Jim Balter
Jim Balter

Reputation: 16406

a warning to alert users to avoid using chars as index of matrix because they can be signned, but obviously this is not the case

Actually, it is the case ... what you see is not what the compiler sees.

is it related with the fact that isspace expects an int as argument?

Yes; isspace is a macro which (in your compiler implementation) accesses an array ... take a look at your ctype.h or ask your compiler to expand macros (e.g., gcc -E) and you will see the array access.

To avoid the warning, use

if (!isspace((unsigned char)c))

Upvotes: 1

Related Questions