Comparison between pointer and integer in C

后端 未结 8 1360
盖世英雄少女心
盖世英雄少女心 2021-01-05 06:00

I have a bit stupid question about program in C. My compiler says me: warning: comparison between pointer and integer. I really don\'t know why. I only want to writ

8条回答
  •  Happy的楠姐
    2021-01-05 06:07

    str[i] is a character. NULL is a pointer. You can't meaningfully compare those two data types (although they may or may not be implemented as the same size of integer internally). That's all the error message means.

    Not that the comparison is not only type-incorrect, it also doesn't do what you probably mean. You seem to assume that a character array with an incomplete initializer would be automatically terminated with a \0 character, but that rule applies to string literals, not to character arrays you create yourself. As it is, you're invoking undefined behaviour.

提交回复
热议问题