[英]This code is generating a segment core dump
This code is generating a segment core dump. 这段代码正在生成一个段核心转储。
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;
}
struct linked *temp1 = malloc(sizeof(struct linked));
将内存分配给指针
In 在
struct linked *temp1;
int count=1;
while(count<10){
temp1->val=count;
temp1
is never made to point to anything. temp1
永远不会指向任何东西。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.