Warning comparison between pointer and integer

catee picture catee · Sep 10, 2015 · Viewed 180.8k times · Source

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 then loop through the message and I get an error in the below if statement.

 if (*message == "\0") {
  ...//do something
 }

The error I am getting is:

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

I thought that the * in front of message dereferences message, so I get the value of where message points to? I do not want to use the library function strcmp by the way.

Answer

blakelead picture blakelead · Sep 10, 2015

It should be

if (*message == '\0')

In C, simple quotes delimit a single character whereas double quotes are for strings.