- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是《The c programming language》一书中的程序。
出现错误:“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 ....
最佳答案
您不能拥有自己的名称以 str
开头的函数.整个“命名空间”在 C 中是保留的。
在这种情况下,strdup()
是 <string.h>
的标准函数,您的函数声明与之冲突。
请注意,仅仅停止使用 <string.h>
是不够的, 该名称仍然是保留的,因此您不能有效地使用它。
一些进一步的注意事项:
const
指针。malloc()
in C .strdup()
workalike 严重损坏,它调用 strcmp()
当它意味着 strcpy()
.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()
因为我知道长度,所以可以更快。
关于c - 错误 : conflicting types for 'strdup' !,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21850356/
与 strdup 一起工作时在 Windows 上我发现 _strdup是 Windows 特定的,但是当我在 Linux 上运行相同的代码时,它需要 strdup没有下划线。有谁知道这种差异背后的历
我正在阅读“操作系统:三个简单的部分”。在第 5 章中,有一段代码展示了 exec() 系统调用的用法。 1 #include "common.h" 2 3 int main(
关闭。这个问题需要details or clarity .它目前不接受答案。 想要改进这个问题吗? 通过 editing this post 添加详细信息并澄清问题. 关闭 2 年前。 Improve
我调用 strdup 来复制 set_device( devname ) 中的“card”字符串set_device( 设备名称 )然后我使用“card”打开混音器: devname 的格式为 hw:
我有一个用 C 编写的应用程序。我在那里使用 strdup 来复制 char*。在调用 strdup 之前,我正在验证源字符串。即使 strdup 正在转储核心,它也不等于 NULL。 这是回溯 #0
可能这是非常基本的问题,每个人都会对我大喊大叫,但我已经尝试解决这个问题几个小时了,但再也无法忍受了。我有这个结构 struct node { Key_Type element; tree_ptr l
我必须分配一个包含 1000 个字符串指针的数组,从 stdin 中读取每个字符串,并将每一行的 strdup 读入数组中。我做了以下事情: char *array[1000]; int index
我在线程函数中使用下面的 get_sessionid() 函数,然后尝试释放该指针。 static char sessionid[SESSIONID_LEN] = { '\0' }; static v
我正在实现一个 strdup 函数作为练习。 char* strdupPtr(const char* str) { const size_t sz{strlen(str)+1}; ch
我正在尝试这段代码: imei=Found_imei(pClient->GetBuffer()); printf("6. CODICE IMEI %s \n",imei); pClient->SetI
我实现了一个返回字符串的函数。它接受一个整数作为参数 (age),并返回一个格式化的字符串。 除了我有一些疯狂的内存泄漏之外,一切都运行良好。我知道 strdup() 是造成这种情况的原因,但我试图研
如果输入的 const 字符串以某种方式被修改(这会导致 C 编译器警告),那么处理它的最佳方法是什么 - 将其类型转换为新变量然后使用它或复制它并使用它然后然后释放它。或者有没有其他方法可以处理这种
在我从前人那里继承的程序中,有以下格式的功能: somefunc(some_type some_parameter, char ** msg) 换句话说,最后一个参数是一个char **,用来
我正在为一本书编写一个 C++ 类,其中包含一个名字: class Book { private: char* nm; .......... ............ .......... .
我将 VTK 与 MSVC 一起使用,并在尝试加载数据时出现奇怪的行为。我对它进行了一些修改,甚至下面的代码也会产生堆损坏,知道发生了什么或可能出了什么问题吗? vtkAbstractArray *v
我有这样的类(class): class Kot{ public: string name; }; 我创建了它的一个实例: Kot* kot = new Kot; kot->name = "J
strdup 是否每次都分配另一个内存区域并创建另一个指针? 例如:下面的代码会导致内存泄漏吗? void x(char** d, char* s){ *d = strdup(s); } in
当我在做作业时,我开始知道我们不应该使用这样的作业: char *s="HELLO WORLD"; 使用这种语法的程序很容易崩溃。 我试过并使用过: int fun(char *temp) {
我正在调用 strdup 并且必须在调用 strdup 之前为变量分配空间。 char *variable; variable = (char*) malloc(sizeof(char*)); var
我最近意识到我在 OS X 上经常使用的 strdup() 函数不是 ANSI C 的一部分,而是 POSIX 的一部分。我不想重写我所有的代码,所以我想我只是要编写我自己的 strdup() 函数。
我是一名优秀的程序员,十分优秀!