做练习时, gets(str)!=NULL 被编译器警告说 "compartion between pointer and integer",说明gets()返回值变为int
具体代码如下
#include<stdio.h>
#define MAX 80
void delspace(char * p1);
int main(void){
char str[MAX];
puts("Please enter the string:");
//gets(str);
//while(*str != '\0'){
while(gets(str)!=NULL){
delspace(str);
puts("The result is:");
puts(str);
puts("Please enter other string:");
// gets(str);
}
puts("Done!");
return 0;
}
编译器版本
gcc version 6.2.0 20161005 (Ubuntu 6.2.0-5ubuntu12)
在 user/include/stdio.h中的定义,但是我没找到NULL的定义
extern char *gets (char *__s) __wur __attribute_deprecated__;
后来我做了两个测试
test.c:6:11: warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int’ [-Wformat=]
printf("%p",gets(name));
test.c:6:11: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘void *’ [-Wformat=]
printf("%d\n",NULL);
贴出你完整的代码来看。以及说明你用的编译器和版本。
char *gets( char* buffer );wchar_t *_getws( wchar_t* buffer );
C++ 的定义 #define NULL 0
所以是 gets() 返回的 char *
这个 pointer 和 NULL 这个 int 比较。
具体代码如下
#include<stdio.h>
#define MAX 80
void delspace(char * p1);
int main(void){
char str[MAX];
puts("Please enter the string:");
//gets(str);
//while(*str != '\0'){
while(gets(str)!=NULL){
delspace(str);
puts("The result is:");
puts(str);
puts("Please enter other string:");
// gets(str);
}
puts("Done!");
return 0;
}
编译器版本
gcc version 6.2.0 20161005 (Ubuntu 6.2.0-5ubuntu12)