今天学习c语言的时候,写K&R书里面的一个例子,用到了strdup这个函数,书中是自己实现的,由于C语言水平有限,完全不知道这个函数。
程序里面这个函数式这样的:
char *strdup(char *s)
{
char *p;
p = (char *) malloc(strlen(s) + 1);
if ( p != NULL)
strcpy(p,s);
return p;
}
所以写上去了之后用gcc 编译,提示是这样的:
6_5.c:81: error: conflicting types for ‘strdup’
我没意识到这个是库函数,仔细看了看,也没意识到哪出错。然后我忽然想用clang试一下。提示是这样的:
lvlv@lvlv-laptop:~/kr$ clang 6_5.c
6_5.c:81:7: warning: incompatible redeclaration of library function 'strdup'
char *strdup(char *s)
^
In file included from 6_5.c:2:
/usr/include/string.h:173:14: note: 'strdup' is a builtin with type 'char *(char const *)'
extern char *strdup (__const char *__s)
^
6_5.c:92:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
3 diagnostics generated.
差距实在是太明显了..........
clang提示的比gcc详细太多了。