Warning: comparison between pointer and integer in C

I get a warning

warning: comparison between pointer and integer

in a line containing if from the following code snippet:

 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?

+2
source share
5 answers

Do you enable unistd.h? If not, an error appears because your C compiler assumes getcwd returns an int.

To fix? Include unistd.h

+8
source

The prototype of getcwd is

 char *getcwd(char *buf, size_t size); 

Make sure you include <unistd.h> , otherwise the default return type will be int .

Here, even Ideone gives the Current Working Directory

+4
source

Did you include .h so that the compiler understands what getcwd returns?

the behavior of your c compilers should probably take an int return value from each undefined function.

0
source

In the return types section of the following link, getcwd returns null on error. So instead of checking for != (char *)NULL just check != NULL

http://linux.die.net/man/3/getcwd

-1
source

Change the line with this if (getcwd(cwd, sizeof(cwd)) != NULL)

-1
source

Source: https://habr.com/ru/post/1441699/


All Articles