Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incompatible integer to pointer conversion errors?

Tags:

c

pointers

For some reason, I'm getting an error at this line:

while ((en = strtok(NULL, " " ) !=NULL)){ //do something }

and at this line (the error for this one is 'comparison between pointer and integer ('int' and 'void *'), even though inputString is a char array and null is null.

while (!inputString[i]==NULL)

en is a char, and was declared as char *en. I'm not sure why...is it because I can't compare them with NULL?

like image 263
user2253332 Avatar asked Jun 17 '26 09:06

user2253332


1 Answers

The problem isn't the comparison - it's the assignment. != has higher precedence than =, so your expression is parsed as:

en = (strtok(NULL, "") != NULL)

presumably en is a pointer type, and the result of != is an int, so that's where the warning arises. You probably meant:

(en = strtok(NULL, "")) != NULL

as the condition. The same is true in your second example - ! has higher precedence than ==, so you're comparing the result of !inputString[i] (which has type int) to NULL (which may have type void *). You might have meant:

while (!(inputString[i] == NULL))

which can also be written as:

while (inputString[i] != NULL)

or just

while (inputString[i])
like image 170
caf Avatar answered Jun 19 '26 03:06

caf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!