我有 2 个使用指针形成链表的结构。
typedef struct {
char *text;
int count;
} *Item;
typedef struct node {
Item item;
struct node *next;
} *link;
我试图在这种类型上创建一堆操作,但我在使用这个特定函数时遇到了很多问题。
error: request for member ‘text’ in something not a structure or union strcpy(new->item->text, buffer->text);
error: request for member ‘text’ in something not a structure or union new->item->text = (char*) malloc(sizeof(char)*(strlen(buffer->text)+1));
基本上错误是在buffer->text pass 上,但过去一个小时我一直在弄乱它,似乎找不到问题所在。我可能遗漏了一些明显的东西,但我再也听不懂了。
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;
}
最佳答案
问题是您有两层间接寻址,而不是只有一层。具体来说,buffer
的类型是 Item *
,而 Item
是一个 struct {...} *
。您可能希望将 Item *buffer
更改为仅 Item buffer
。如果你真的需要两层间接寻址,那么你需要做 (*buffer)->text
来访问 text
字段,但这可能不是你想要的想要。
这也是很多人反对using typedef
to define pointer types的原因之一,因为它经常会导致这样的错误。
关于c - 错误 : request for member ‘****’ in something not a structure or union,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37223427/