C 错误:';strdup';!
这是《c编程语言》一书中的程序。C 错误:';strdup';!,c,strdup,C,Strdup,这是《c编程语言》一书中的程序。 出现错误:“strdup”的类型冲突!遇到函数“strdup”时。但如果将“strdup”更改为其他名称,例如“strdu”,则错误将消失。 我不知道为什么?顺便说一下,我使用code::blocks作为IDE #include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #define MAXWORD 100 str
出现错误:“strdup”的类型冲突!遇到函数“strdup”时。但如果将“strdup”更改为其他名称,例如“strdu”,则错误将消失。
我不知道为什么?顺便说一下,我使用code::blocks作为IDE
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAXWORD 100
struct tnode {
char *word;
int count;
struct tnode *left;
struct tnode *right;
};
struct tnode *addtree(struct tnode *, char *);
struct tnode *talloc(void);
void treeprint(struct tnode *);
int getword(char *, int);
char *strdup(char *);
/* word frequency count */
int main()
{
struct tnode *root;
char word[MAXWORD];
root = NULL;
while (getword(word, MAXWORD) != EOF)
if (isalpha(word[0]))
root = addtree(root, word);
treeprint(root);
return 0;
}
/* addtree: add a node with w, at or below p */
struct tnode *addtree(struct tnode *p, char *w)
{
int cond;
if (p == NULL) { /* a new word has arrived */
p = talloc(); /* make a new node */
p->word = strdup(w);
p->count = 1;
p->left = p->right = NULL;
} else if ((cond = strcmp(w, p->word)) == 0)
p->count++; /* repeated word */
else if (cond < 0) /* less than into left subtree */
p->left = addtree(p->left, w);
else /* greater than into right subtree */
p->right = addtree(p->right, w);
return p;
};
/* treeprint: in-order print of tree p */
void treeprint(struct tnode *p)
{
if (p != NULL) {
treeprint(p->left);
printf("%4d %s\n", p->count, p->word);
treeprint(p->right);
}
}
/* talloc: make a tnode */
struct tnode *talloc(void)
{
return (struct tnode *) malloc(sizeof(struct tnode));
};
char *strdup(char *s) /* make a duplicate of s */
{
char *p;
p = (char *) malloc(sizeof(strlen(s)) + 1);
if (p != NULL)
strcmp(p, s);
return p;
}
.... some other function ....
#包括
#包括
#包括
#包括
#定义MAXWORD 100
结构节点{
字符*字;
整数计数;
结构tnode*左;
结构tnode*右侧;
};
struct tnode*addtree(struct tnode*,char*);
结构tnode*talloc(无效);
void treeprint(struct tnode*);
int getword(char*,int);
char*strdup(char*);
/*词频计数*/
int main()
{
结构tnode*根;
字符字[MAXWORD];
root=NULL;
while(getword(word,MAXWORD)!=EOF)
if(isalpha(字[0]))
root=addtree(root,word);
treeprint(根);
返回0;
}
/*addtree:添加一个w、p或p以下的节点*/
struct tnode*addtree(struct tnode*p,char*w)
{
int-cond;
如果(p==NULL){/*一个新词已经到达*/
p=talloc();/*创建一个新节点*/
p->word=strdup(w);
p->count=1;
p->left=p->right=NULL;
}如果((cond=strcmp(w,p->word))=0)
p->count++;/*重复单词*/
如果(cond<0)/*小于左子树*/
p->left=addtree(p->left,w);
else/*大于右子树*/
p->right=addtree(p->right,w);
返回p;
};
/*树打印:按顺序打印树p*/
void treeprint(struct tnode*p)
{
如果(p!=NULL){
树打印(p->左);
printf(“%4d%s\n”,p->count,p->word);
树打印(p->右);
}
}
/*塔洛克:做一个tnode*/
结构tnode*talloc(无效)
{
return(struct tnode*)malloc(sizeof(struct tnode));
};
char*strdup(char*s)/*复制s*/
{
char*p;
p=(char*)malloc(sizeof(strlen))+1;
如果(p!=NULL)
strcmp(p,s);
返回p;
}
.... 其他一些功能。。。。
strdup在string.h中定义为allready。只需重命名您的函数。您不能拥有名称以str
开头的函数。整个“名称空间”在C中是保留的
在本例中,strdup()
是
中的标准函数,函数声明与之冲突
请注意,仅停止使用
,名称仍然保留,因此您无法有效地使用它
还有几点需要注意:
const
指针strdup()
worklike严重损坏,当它的意思是strcpy()
时,它会调用strcmp()
sizeof(strlen(s))
是完全错误的,即使您修复了strcmp()
/strcpy()
问题,也会导致大量问题strdup()
实现是:
char * my_strdup(const char *s)
{
char *r = NULL;
if(s != NULL)
{
const size_t size = strlen(s) + 1;
if((r = malloc(size)) != NULL)
memcpy(r, s, size);
}
return r;
}
我使用
memcpy()
,因为我知道长度,它可以更快。另外,s
应该声明为const char*
,因为它不会修改字符串的内容(编辑:Ninja'd by unwind's Edit添加了代码)。@Medinoc这当然是正确的(在我的代码中是“自动的”),但我也明确提到了这一点。谢谢。@放松点,我对“名称空间”考虑得很少,显然我不熟悉。谢谢你告诉我这些和更多的笔记!