c语言,输出链表有一处不懂


#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就正确了,求解答,谢谢

我没有细看代码,但是你的代码本身就有问题,我主要看了这两个地方,其他有问题你自己找吧

scanf("%s,str1);

请改为

scanf("%s",str1);

然后

printf("%s",*(pprint->str));

请改为

printf("%s",pprint->str);

实际上,两种应该都可以,printf也应当调用指针


scanf不能接受空格、制表符Tab、回车等;
而gets能够接受空格、制表符Tab和回车等;
scanf :当遇到回车,空格和tab键会自动在字符串后面添加’\0’,但是回车,空格和tab键仍会留在输入的缓冲区中。
gets:可接受回车键之前输入的所有字符,并用’\0’替代 ‘\n’.回车键不会留在输入缓冲区中

#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 =(struct damo*) 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 = (struct demo*)(pprint->pnext);

        if (pprint == NULL)
        {
            break;
        }
        printf("%s", (pprint->str));
    }

}


你再试试