彭格列XI代 发表于 2019-12-26 10:58:25

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

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

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


struct school
{
        int num;
        char name;                                                    //这里如果改成char *name,下面运行到输出name的时候会出问题。
        char sex;
        char job;
        union
        {
                int banji;
                char position;      
        }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;

        }
}

wp231957 发表于 2019-12-26 11:03:42

越界了,没有分配空间空间指向不对等等

Tec 发表于 2019-12-26 11:22:20

scanf("%10s", (*p1).name);

这里写错了第二个参数应该传入地址,指针本身就是地址了,不用再加上取地址符号&
数组名name代表首地址,"&name"代表数组的地址,这2个值恰好相同所以传入数组没问题

彭格列XI代 发表于 2019-12-26 15:43:14

Tec 发表于 2019-12-26 11:22
scanf("%10s", (*p1).name);

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

改了,输入过程直接停止工作了,想不通emmmm

彭格列XI代 发表于 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);
}

这样同样在输入过程,程序直接停止工作了

Tec 发表于 2019-12-26 16:23:25

应该是指针需要提前分配好相应的足够的内存空间,而数组已经提前给定分配好空间了
页: [1]
查看完整版本: 为什么我用字符串指针,输出的时候会停止程序