c - 从不同结构类型中的指针向结构指针赋值

标签 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;

最佳答案

字段 right在结构中rect不应该有 struct之前 totherightstruct _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;

关于c - 从不同结构类型中的指针向结构指针赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20244440/

相关文章:

python - sys.path.append() 是一个好的解决方法吗?

c - 将结构传递给函数

c - Free() 上的 HeapFree 断点

c - 运行时检查失败 #3 - 变量 'name_t' 未经初始化就被使用

类上的 C++ 容器,返回指针

c++ - 将函数指针返回到在 main() 中初始化的对象

C 整数关闭,可能溢出

c - 为什么逗号运算符的工作方式会有所不同?

java - 有人可以解释一下这个链表空指针异常吗?

c++ - 如何访问另一个文件中的结构数组