Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning comparison between pointer and integer

Tags:

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.

like image 777
catee Avatar asked Sep 10 '15 19:09

catee


People also ask

What is warning comparison between pointer and integer in C?

The warning means that you are trying to compare a character with a string, hence the "comparison between pointer (string) and integer (character)" message.

Can we compare pointer and integer?

But seriously, you should never, ever, ever, ever, ever compare a pointer with an integer. They are not the same thing. In languages like C and C++, the size of a pointer and the size of an int are never guaranteed to be the same.

Is eof a pointer or an integer?

EOF is a macro for a negative integer constant. fp is a pointer to FILE . you compare a pointer with an integer of a non-zero value, which isn't permissible.


2 Answers

It should be

if (*message == '\0') 

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

like image 126
blakelead Avatar answered Sep 21 '22 18:09

blakelead


This: "\0" is a string, not a character. A character uses single quotes, like '\0'.

like image 26
dbush Avatar answered Sep 19 '22 18:09

dbush