从不同结构类型中的指针指向结构的指针

时间:2013-11-27 13:58:58

标签: c pointers structure variable-assignment

我目前正在尝试为一个名为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;

1 个答案:

答案 0 :(得分:1)

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

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;