warning: comparison between pointer and integer in C

前端 未结 5 1291
栀梦
栀梦 2021-01-23 16:39

I get a warning

warning: comparison between pointer and integer

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

         


        
相关标签:
5条回答
  • 2021-01-23 16:51

    The prototype of getcwd is

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

    Make sure you include <unistd.h> otherwise the return type would default to int.

    Here, even Ideone gives its Current Working Directory

    0 讨论(0)
  • 2021-01-23 16:55

    have you included the .h necessary so that the compiler understands what getcwd returns?

    the behavior of your c compilers is probably to assume an int return value from every undefined function.

    0 讨论(0)
  • 2021-01-23 16:56

    Modify line with this one if (getcwd(cwd, sizeof(cwd)) != NULL)

    0 讨论(0)
  • 2021-01-23 16:58

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

    The fix? Include unistd.h

    0 讨论(0)
  • 2021-01-23 17:17

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

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

    0 讨论(0)
提交回复
热议问题