Char array subscript warning

后端 未结 3 1309
粉色の甜心
粉色の甜心 2020-12-07 01:51

when I use char array subscript as in this example:

int main(){
    char pos=0;
    int array[100]={};

    for(pos=0;pos<100;pos++)
        printf(\"%i\\         


        
3条回答
  •  隐瞒了意图╮
    2020-12-07 02:58

    Plainly put, it's complaining about your index being of type character.

    Simply convert pos to an int and the warning will disappear.

    As for why this warning appears, you've stated it yourself. A char datatype can only address values from 0 - 255 (if unsigned) or even just 127 (if signed). For a normal array, that's a rather meager number and you could quickly forget about the signed bit and accidentally access array location -128 (by actually accessing array element 128).

    As long as your array is smaller than 127, you'll be fine and you can ignore the warning, but let's be honest...these three extra bytes in your memory footprint won't make your application inefficient, will it?

提交回复
热议问题