双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

为什么给链表元素初始化,程序就崩溃了?!!!!!

helloDesword helloDesword 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
typedef struct node{
char a[4];
struct node *link;
}NODE;

void print(NODE *p)
{
int i;
for(;p->link!=NULL;p=p->link)
{for(i=0;i<4;i++)printf("%c ",p->a[i]);}
}

void main()
{
NODE *head,*p;
int i;
head=(NODE *)malloc(sizeof(NODE));
p=head;
printf("input the linklist value:\n");

while(1)
{ for(i=0;i<4&&p->a[i]!='\0';i++)scanf("%c",&p->a[i]);
if(p->a[i]=='\0')break;
p=p->link=(NODE *)malloc(sizeof(NODE));
}

p->link=NULL;
printf("these are you input values:\n");
print(head);
}
为什么运行后我输入字符,程序就自己崩溃了?可能while 语句有问题,也或许别的地方。
请大神解答。感激不尽!!!
...全文
评论 6
收藏 已收藏
分享
返回顶部
helloDesword
C语言 70,039 成员 243,237 内容