c - Char array subscript warning -


when use char array subscript in example:

int main(){     char pos=0;     int array[100]={};      for(pos=0;pos<100;pos++)         printf("%i\n", array[pos]);      return 0; } 

i getting warning using char array subscript:

warning: array subscript has type ‘char’ [-wchar-subscripts]

which ok, because have warning enabled.

gcc manual says:

-wchar-subscripts warn if array subscript has type "char". common cause of error, programmers forget type signed on machines. warning enabled -wall.

so warning should prevent of using negative array index. question is, why warning active on char , not on other signed types?

thank you.

this because int signed.

char doesn't have to.

char can signed or unsigned, depending on implementation. (there 3 distinct types - char, signed char, unsigned char)

but what's problem? can use values 0 127. can hurt me silently?

oh, yes can.

//depending on signedess of char, //either correct loop, //or loop infinitely , write on memory char an_array[50+1]; for(char = 50; >= 0; i--) {     an_array[i] = i;     // if char unsigned, variable can never < 0     // , loop infinitely } 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -