初始化结构后C中的分段错误

时间:2020-03-02 21:22:20

标签: c

为什么在添加任务t2的代码后出现“分段错误(核心已转储)”错误? 如果删除这些行,我不会收到错误消息。我正在创建一个Task,以将其分配给节点结构中的指针变量。插入和遍历功能起作用,并且t1的变量值被打印出来。但前提是我删除了任务t2的五行代码。

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

typedef struct {
    char *name;
    int tid;
    int priority;
    int burst;
} Task;

void insert(struct node **a, Task *b);
void traverse(struct node *a);


int main(int argc, char *argv[])
{
    Task *t1;
    t1->name = "t1";
    t1->tid = 2000;
    t1->priority = 1;
    t1->burst = 20;

    struct node *first = NULL;  
    insert(&first,t1);
    traverse(first);

    Task *t2;
    t2->name = "t1";
    t2->tid = 2000;
    t2->priority = 1;
    t2->burst = 20;


    return 0;
}

1 个答案:

答案 0 :(得分:0)

请为t1和t2结构指针分配一些内存,或将它们指向其他相同结构对象的有效地址。你不能那样使用它。