警告:C中指针和整数之间的比较
我收到警告
warning: comparison between pointer and integer
在包含if
下一段代码的行上:
char cwd[1024]; if (getcwd(cwd, sizeof(cwd)) != (char*)NULL) printf("%s\n",cwd); else error_print("error in pwd");
我需要修复的方式和内容?
你包括unistd.h吗? 如果没有,则出现错误,因为您的C编译器假设getcwd返回int。
修复? 包括unistd.h
getcwd的原型是
char *getcwd(char *buf, size_t size);
确保包含
否则返回类型将默认为int
。
在这里,即使Ideone也提供其当前的工作目录
你是否包含.h必要的,以便编译器了解getcwd返回的内容?
你的c编译器的行为可能是假定每个未定义函数的int返回值。
在以下链接的返回类型部分中,getcwd在失败时返回null。 因此,不要检查!= (char *)NULL
只需检查!= NULL
修改一行if (getcwd(cwd, sizeof(cwd)) != NULL)