1

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?

SIMEL
  • 8,246
  • 23
  • 76
  • 115

5 Answers5

8

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

The fix? Include unistd.h

Dr. Snoopy
  • 50,080
  • 6
  • 102
  • 121
  • i am not getting any error but just warning only , is there any prototype def in stdio.h or stdlib.h ----------------------------- int getcwd(char *buf, size_t size); – cc4re Mar 31 '13 at 11:43
4

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

Sadique
  • 21,741
  • 6
  • 59
  • 89
0

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.

KevinDTimm
  • 13,876
  • 3
  • 39
  • 59
-1

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

Thebigcheeze
  • 3,197
  • 2
  • 20
  • 18
-1

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

Priyank
  • 9,883
  • 2
  • 24
  • 24