warning: comparison between pointer and integer in C

SIMEL picture SIMEL · Apr 13, 2011 · Viewed 7.6k times · Source

I get a warning

warning: comparison between pointer and integer

on the line containing if from the next piece of code:

char cwd[1024];

if (getcwd(cwd, sizeof(cwd)) != (char*)NULL)
    printf("%s\n",cwd);
else
    error_print("error in pwd");

how and what do I need to fix?

Answer

Dr. Snoopy picture Dr. Snoopy · Apr 13, 2011

Do you include unistd.h? If not, the error appears because your C compiler is assuming getcwd returns int.

The fix? Include unistd.h