Warning comparison between pointer and integer

后端 未结 3 2039
无人共我
无人共我 2021-02-04 01:57

I am getting a warning when I iterate through the character pointer and check when the pointer reaches the null terminator.

 const char* message = \"hi\";

 //I         


        
3条回答
  •  醉话见心
    2021-02-04 02:16

    In this line ...

    if (*message == "\0") {
    

    ... as you can see in the warning ...

    warning: comparison between pointer and integer
          ('int' and 'char *')
    

    ... you are actually comparing an int with a char *, or more specifically, an int with an address to a char.

    To fix this, use one of the following:

    if(*message == '\0') ...
    if(message[0] == '\0') ...
    if(!*message) ...
    

    On a side note, if you'd like to compare strings you should use strcmp or strncmp, found in string.h.

提交回复
热议问题