#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct demo
{
char str[15];
struct damo* pnext;
};
struct demo* create()
{
char str1[15];
struct demo* pnew,*pend,*phead;
pnew=pend=(struct demo*)malloc(sizeof(struct demo));
phead=pnew;
printf("请输入文字");
gets(str1);
strcpy(phead->str,str1);
if(*str1)
{
for(;;)
{
pnew=(struct demo*)malloc(sizeof(struct demo));
pend->pnext=pnew;
printf("请输入文字");
gets(str1);
strcpy(pnew->str,str1);
if(!*str1)
{
break;
}
pend=pnew;
}
}
pend->pnext=NULL;
free(pnew);
return phead;
}
void main()
{
struct demo* phead;
struct demo* pprint;
phead=create();
printf("\n输出为:\n");
pprint=phead;
puts(pprint->str);
for(;;)
{
pprint=pprint->pnext;
if(pprint==NULL)
{
break;
}
puts(pprint->str);
}
}
上面是正确的版本,下面是错误的版本
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct demo
{
char str[15];
struct damo* pnext;
};
struct demo* create()
{
char str1[15];
struct demo* pnew,*pend,*phead;
pnew=pend=(struct demo*)malloc(sizeof(struct demo));
phead=pnew;
printf("请输入文字");
scanf("%s",*str1);
strcpy(phead->str,str1);
if(*str1)
{
for(;;)
{
pnew=(struct demo*)malloc(sizeof(struct demo));
pend->pnext=pnew;
printf("请输入文字");
scanf("%s,str1);
strcpy(pnew->str,str1);
if(!*str1)
{
break;
}
pend=pnew;
}
}
pend->pnext=NULL;
free(pnew);
return phead;
}
void main()
{
struct demo* phead;
struct demo* pprint;
phead=create();
printf("\n输出为:\n");
pprint=phead;
printf("%s",*(pprint->str));
for(;;)
{
pprint=pprint->pnext;
if(pprint==NULL)
{
break;
}
printf("%s",*(pprint->str));
}
}
这两个程序就有一个不同,就是正确的程序中用的是puts和gets,错误的程序中用的printf和scanf,我不太清楚为什么把printf和scanf换成puts和gets就正确了,求解答,谢谢