亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么我的程序運行的時候顯示Segmentation fault (core dumped)

#include <stdio.h>
#include <malloc.h>
struct weapon {
?????? int price;
?????? struct weapon *next;
};
?struct weapon *create(){
?struct weapon *p1,*p2,*head;
?p2=p1=(struct weapon*)malloc(sizeof(struct weapon));
?return 0;
?scanf("%d",&p1->price);
?head=NULL;
?int n=0;
?while(p1->price != 0){
???? n++;
???? if(n==1) head=p1;
???? else p2->next=p1;
?
???? p2=p1;
???? p1=(struct weapon*)malloc(sizeof(struct weapon));
???? scanf("%d",&p1->price);
}
?p2->next = NULL;
?return (head);
}
int main(){
struct weapon *p;
?p=create();
?printf("%d",p->price);
return 0;
}

正在回答

1 回答

create函數的返回類型是struct weapon指針。

可在函數中,一開始聲明了p1和p2兩個指針,然后申請了一段內存,讓p1和p2指向它。可緊接著就return 0; 了,函數就返回了!并且返回的是個NULL指針! ?后面的代碼完全沒用到。

在main中,p得到的就是個空指針,訪問空指針是不允許的! 所以后面printf在試圖訪問空指針時就引起了segmentation fault

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么我的程序運行的時候顯示Segmentation fault (core dumped)

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號