c - 错误 : request for member ‘****’ in something not a structure or union

标签 c arrays pointers struct linked-list

我有 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/

相关文章:

arrays - 算法,以便我可以以某种方式索引 2^n 组合,这样我就可以在不使用数组的情况下从 1 的任何索引值回溯到 2^n

将指针从帧缓冲区转换到另一个地址

c - 在 C 中用 gets 读取一个 txt 文件

C 数组赋值使用大括号语法

C:返回调用函数时变量值变脏

c - ANSI C 警告 : assignment from incompatible pointer type

python - 为什么 Python 不操作这个数组?

c++ - c++ 中是否可能存在指向不同类型的指针数组?

c - 在 C 中返回一个指针(段错误故障排除)

c - 如何识别硬盘使用的协议(protocol)?