此代码生成段核心转储

时间:2014-12-04 17:20:18

标签: c linked-list

此代码正在生成 细分核心转储。

typedef struct linked{
        int val;
        struct linked *index;
}linked;


    struct linked *temp1;

     int count=1;
     while(count<10){

           temp1->val=count;
           temp1=temp1->index;

           count++;

     } //end of while

     while(temp1!=NULL){
           printf(" %d\n",temp1->val);
           temp1=temp1->index;
     }

3 个答案:

答案 0 :(得分:1)

struct linked *temp1 = malloc(sizeof(struct linked));

将内存分配给指针

答案 1 :(得分:1)

struct linked *temp1;

int count=1;
while(count<10){

      temp1->val=count;

temp1永远不会指向任何事物。

答案 2 :(得分:-1)

struct linked *temp1, *top;
int count;

temp1 = top = calloc(1, sizeof(*top));
for(count=1;count<10;count++){
    temp1->val = count;
    if(count < 10 -1)//not last
        temp1 = temp1->index = calloc(1, sizeof(*temp1));
}
temp1 = top;
 while(temp1!=NULL){
    printf(" %d\n",temp1->val);
    temp1=temp1->index;
}
相关问题