為什么我的結(jié)果不是1,1而是3,3
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct weapon
{
int price;
int damage;
struct weapon *next;
};
struct weapon *create()
{
struct weapon *p1,*p2,*head;
int i=0;
p1 = p2 = (struct weapon*)malloc(sizeof(struct weapon));
head = NULL;
scanf_s("%d %d",&p1->price,&p1->damage);
while(0!=p1->price)
{
i++;
if( i = 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct weapon*)malloc(sizeof(struct weapon));
scanf_s("%d %d",&p1->price,&p1->damage);
}
p2->next = NULL;
return (head);
}
int main(void)
{
struct weapon *p;
p = create();
printf("%d,%d",p->price,p->damage);
system("pause");
return 0;
}
2017-08-23
建議把i==1寫成1==i,這種錯誤就可以避免了,至少系統(tǒng)會提醒你錯了,不能給常量賦值,,,,,,,望采納
2017-08-08
我的問題都有點傻QAQ,if( i = 1)這種寫法導致i始終是1然后head=p1一直執(zhí)行,所以沒有產(chǎn)生鏈表,只是把所有輸入的值都賦值給head了