C - 结构传递值 - 段错误

标签 c struct

尝试将值放入标签时,list->qtag->label=x; 中的错误在哪里?

struct quad{

        int label;
        char op[30];
        char x[30];
        char y[30];
        char z[30];
        struct quad *next;
};

struct tag{

        struct quad *qtag;
        struct tag *next2;
};

struct tag *makelist(int x){

        struct tag *list;
        list=(struct tag*)malloc(sizeof(struct tag));
        list->qtag->label=x;
        printf("!\n");
        return list;
};

最佳答案

您没有在列表中初始化该qtag。可以这样修复:

list=(struct tag*)malloc(sizeof(struct tag));
list->qtag = (struct quad*)malloc(sizeof(struct quad));
list->qtag->label = x;

不要忘记检查 malloc() 的返回值是否存在可能的错误。

关于C - 结构传递值 - 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23456126/

相关文章:

C++ - 黑桃 - 类 : How to use them

我可以使用 C 指针结构函数更改此 C 代码吗?

c# - 将值分配给 C++ 中的结构到从 C# 传递的结构时出现问题

c - 为什么要在 C 中将 extern 结构体传递给函数?

C: 在 printf 语句中将 char* 传递给 %s

c - 如何使用 C 头文件和 CUDA 代码编译 C 代码?

c程序for循环: execution sequence

c++ - 如何与 libtiff 链接

c - 私有(private)变量与减少 OMP

c - 在结构数组上重新分配,索引时地址边界错误