鱼C论坛

 找回密码
 立即注册
查看: 1030|回复: 5

[已解决]为什么我用字符串指针,输出的时候会停止程序

[复制链接]
发表于 2019-12-26 10:58:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 彭格列XI代 于 2019-12-26 11:01 编辑

#include <stdio.h>
#include <stdlib.h>


struct school
{
        int num;
        char name[11];                                                    //这里如果改成char *name,下面运行到输出name的时候会出问题。
        char sex;
        char job;
        union
        {
                int banji;
                char position[11];      
        }category;

        struct school *next;

}person;



void main()
{
        struct school *creat();
        void print(struct school *head);

        struct school *stu;

        stu = creat();
        print( stu );

}



struct school *creat()
{
        void judge(struct school *p1);
       
        int i;
        char ch;
        struct school *p1, *p2, *head;

        head = p1 = &person;
       
        printf("Please input the num: ");
        scanf("%d", &(*p1).num);
        printf("Please input the name: ");
        scanf("%10s", &(*p1).name);
       
        while((ch = getchar()) != EOF && ch != '\n')
        {
                ;
        }

        printf("Please input the sex<M/F>: ");
        scanf("%c", &(*p1).sex);
        judge( p1 );


        i = 0;
        while( i < 1 )
        {
                p2 = p1;
                p1 = (struct school *)malloc(sizeof(struct school));
                (*p2).next = p1;


                while((ch = getchar()) != EOF && ch != '\n')
                {
                        ;
                }
               
                printf("\nPlease input the num: ");
                scanf("%d", &(*p1).num);
                printf("Please input the name: ");
                scanf("%10s", &(*p1).name);
               
                while((ch = getchar()) != EOF && ch != '\n')
                {
                        ;
                }

                printf("Please input the sex<M/F>: ");
                scanf("%c", &(*p1).sex);
                judge( p1 );

               
                i++;

        }

       
        (*p1).next = NULL;


        return head;

}


void judge(struct school *p1)
{
        char ch;

        while((ch = getchar()) != EOF && ch != '\n')
        {
                ;
        }

        while(1)
        {       
                printf("Please input the job<s/t>: ");
                scanf("%c", &(*p1).job);


                if( (*p1).job == 's' )
                {
                        while((ch = getchar()) != EOF && ch != '\n')
                        {
                                ;
                        }

                        printf("Please input the class: ");
                        scanf("%d", &(*p1).category.banji);
                        break;
                }
                else if( (*p1).job == 't' )
                {
                        while((ch = getchar()) != EOF && ch != '\n')
                        {
                                ;
                        }

                        printf("Please input the position: ");
                        scanf("%10s", &(*p1).category.position);
                        break;
                }
                else
                {
                        while((ch = getchar()) != EOF && ch != '\n')
                        {
                                ;
                        }

                        printf("Input error!!\n");

                }
        }
}


void print(struct school *head)
{

        printf("\n\n\nNo.\tname\t\tsex\tjob\tclass/position\n");


        while( head )
        {
                printf("%d\t", (*head).num);
                printf("%s\t\t", (*head).name);                                                 //如果是字符串指针,运行到这里会莫名其妙停止程序。
                printf("%c\t", (*head).sex);
                printf("%c\t", (*head).job);
                          
                if( (*head).job == 's' )
                {
                        printf("%d\n", (*head).category.banji);
                }
                else
                {
                        printf("%s\n", (*head).category.position);
                }

                head = (*head).next;

        }
}
最佳答案
2019-12-26 16:23:25
应该是指针需要提前分配好相应的足够的内存空间,而数组已经提前给定分配好空间了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-12-26 11:03:42 | 显示全部楼层
越界了,没有分配空间  空间指向不对  等等
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-12-26 11:22:20 | 显示全部楼层
scanf("%10s", (*p1).name);

这里写错了第二个参数应该传入地址,指针本身就是地址了,不用再加上取地址符号&
数组名name代表首地址,"&name"代表数组的地址,这2个值恰好相同所以传入数组没问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-26 15:43:14 | 显示全部楼层
Tec 发表于 2019-12-26 11:22
scanf("%10s", (*p1).name);

这里写错了第二个参数应该传入地址,指针本身就是地址了,不用再加上取地址 ...

改了,输入过程直接停止工作了,想不通emmmm
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-26 15:57:51 | 显示全部楼层
Tec 发表于 2019-12-26 11:22
scanf("%10s", (*p1).name);

这里写错了第二个参数应该传入地址,指针本身就是地址了,不用再加上取地址 ...

我试过了,写了一个小实验程序

#include <stdio.h>

void main()
{
        char *name;
        scanf("%s", name);
        printf("%s", name);
}

这样同样在输入过程,程序直接停止工作了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-26 16:23:25 | 显示全部楼层    本楼为最佳答案   
应该是指针需要提前分配好相应的足够的内存空间,而数组已经提前给定分配好空间了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-5 05:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表