【问题标题】:I cannot typedef a pointer to structure that has already been typedef-ed我不能 typedef 指向已经 typedef 的结构的指针
【发布时间】:2019-10-03 13:24:25
【问题描述】:

我对这些声明的行有点困惑。我想为我的程序创建一个链接列表,但由于某种原因,当我尝试编译它时它一直出错。基本上这将是我的主要节点模型,并且我的列表后面还有另一个结构(没有 TYPEDEF)。编译得很好。我不知道怎么了。

我已经尝试将 typedef 放在 struct student 上。

typedef struct
{
    char name[50];
    int id;
    node next;

}student;

typedef student* node;

typedef struct listR* list;

struct listR
{
    node head,tail;
    int size;

};

错误: 未知类型名称“节点” 警告: 初始化从整数生成指针而不进行强制转换

【问题讨论】:

  • 在哪一行报告了“未知类型名称node”?可能是student 中的行在定义之前使用了node
  • 观察listR和student的区别,在struct listR上我没有任何错误
  • 在第 14 行是下一个节点!

标签: c data-structures struct linked-list typedef


【解决方案1】:

编译器不知道节点是什么,因为您在创建结构后创建节点类型。

你可以做任何一个:

typedef struct node node;

struct node
{
  char name[50];
  int id;
  node* next;
};

告诉编译器节点是什么,

或者

typedef struct node {
    char name[50];
    int id;
    struct node* next;
} node;

【讨论】:

  • 您的两种方式都以某种方式解决了问题!但我也这样做了: typedef struct student * node; typedef struct student{ 字符名[50];内部标识;下一个节点; }
  • 我的解决方案是通用的。很高兴它有帮助。您的最后一个解决方案(在上面的评论中)有效,因为您对结构进行了原型设计,就像在第一个示例中一样。
【解决方案2】:

我会用这个:

struct Node
{
  struct Node *next;
  struct Node *prev;
};

但是我碰巧是那些没有充分理由不喜欢类型定义结构的人之一。在这里阅读更多:https://stackoverflow.com/a/54752982/6699433

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多