【问题标题】:Assignment to pointer to structure from pointer within a different structure type从不同结构类型中的指针分配指向结构的指针
【发布时间】:2013-12-13 05:04:12
【问题描述】:

我目前正在尝试将一个指针分配给一个名为 totheright 的结构(它是一个链表节点)。指向结构/列表的指针当前位于另一个称为矩形的结构中。当我尝试这样做时遇到错误。

currect->right = malloc(sizeof(totheright));
righthead = current->right;

以下是函数中的声明:

rect *currect = NULL;
totheright *righthead = NULL;

标题中的结构定义:

typedef struct _totheright {
    int rect;
    struct _totheright *right;
} totheright;

typedef struct _rect {
    int thisrect;
    struct totheright *right;
    int left;
    double width;
    double height;
    double xcord;
    double ycord;
    struct _rect *next;
} rect;

【问题讨论】:

  • righthead = current->right; 应该是righthead = currect->right;

标签: c pointers structure variable-assignment


【解决方案1】:

结构体rect 中的字段right 不应在totherightstruct _totheright 之前使用struct

typedef struct _rect {
    int thisrect;
    //struct totheright *right; // was
    totheright *right; // fixed
    //struct _totheright *right; // that would be ok as well
    int left;
    double width;
    double height;
    double xcord;
    double ycord;
    struct _rect *next;
} rect;

【讨论】:

  • 解决了我的问题!我不敢相信我错过了。谢谢你,迈克尔!
猜你喜欢
  • 2019-03-18
  • 2018-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多