我有2个结构使用指针形成链表。
typedef struct {
char *text;
int count;
} *Item;
typedef struct node {
Item item;
struct node *next;
} *link;
我试图在这种类型上创建一系列操作,但我对这个特定功能有很多问题。
错误:请求成员'text'不是结构或联合 strcpy(new-> item-> text,buffer-> text);
错误:请求成员'text'不是结构或联合 new-> item-> text =(char *)malloc(sizeof(char)*(strlen(buffer-> text)+1));
基本上错误发生在缓冲区 - >文字传递上,但过去一小时我一直在搞乱它并且似乎无法找到&#39这是错的。我可能会遗漏一些显而易见的东西,但我不能再把它包裹起来了。
link new_item(Item* buffer) {
link new = (link) malloc(sizeof(struct node));
new->item->text = (char*) malloc(sizeof(char)*(strlen(buffer->text)+1));
strcpy(new->item->text, buffer->text);
new->item->count = 1;
new->next = NULL;
return new;
}
答案 0 :(得分:3)
问题是你有两个间接层而不是一个。具体而言,buffer
的类型为Item *
,Item
为struct {...} *
。您可能希望将Item *buffer
更改为Item buffer
。如果你真的需要两个级别的间接,那么你需要(*buffer)->text
来访问text
字段,但这可能不是你想要的。
这是许多人反对using typedef
to define pointer types的原因之一,因为它经常会导致这样的错误。