进无止进 发表于 2016-09-02 00:02

这个程序运行为什么崩溃

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

#define MAX_NAME_LENGTH 20
#define MAX_STUDENT_NUMBER 10
#define MAX_CLASS_NUMBER 10

typedef struct student
{
        char name;
        unsigned short int age;
        unsigned short int class_number;
        unsigned short int chinese_scores;
        unsigned short int math_scores;
        unsigned short int english_scores;
       
}Student;

typedef struct stu_class
{
        Student students;
        unsigned short int class_number;
       
}Stu_Class;

typedef struct class_system
{
        Stu_Class stu_class;
        //unsigned short int class_sum;
}Class_System;

Class_System class_system;

void Init_Class_System()
{
        memset(&class_system,0,sizeof(Class_System));
        printf("class_system initialization success\n");
}

void Add_A_Student_To_Class_System()
{
        char name ={'\0'};
        unsigned short int age;
        unsigned short int class_number;
        unsigned short int chinese_scores;
        unsigned short int math_scores;
        unsigned short int english_scores;
        unsigned short int number;
        printf("please input name,age,class_number(1-9),chinese score,math score, english score and the number(1-9)\n");

        scanf("%s,%d,%d,%d,%d,%d,%d\n",name,&age,&class_number,
                &chinese_scores,&math_scores,&english_scores,&number);

        strcpy(class_system.stu_class.students.name,name);
        class_system.stu_class.students.age = age;
        class_system.stu_class.students.class_number = class_number;
        class_system.stu_class.students.chinese_scores = chinese_scores;
        class_system.stu_class.students.math_scores = math_scores;
        class_system.stu_class.students.english_scores = english_scores;

        printf("Add student info success!\n");
}

int main()
{
        int i;
        Init_Class_System();
        for(i = 0; i<10;i++)
        {
                Add_A_Student_To_Class_System();
        }

        return 0;
}

451006071 发表于 2016-09-02 06:40

因为scanf函数输入错误,导致其他值没有被赋值。然后访问数组成员时越界导致的segment fault。
修改一下scanf就好了。scanf输入%s时需要额外注意,他只能放在所有数字输入之后,或者单独输入一行用于保存文本(name)。%s在最开头表示整行输入数据都给了name,导致其余变量没有正确的值。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NAME_LENGTH 20
#define MAX_STUDENT_NUMBER 10
#define MAX_CLASS_NUMBER 10

typedef struct student
{
      char name;
      unsigned short int age;
      unsigned short int class_number;
      unsigned short int chinese_scores;
      unsigned short int math_scores;
      unsigned short int english_scores;

}Student;

typedef struct stu_class
{
      Student students;
      unsigned short int class_number;

}Stu_Class;

typedef struct class_system
{
      Stu_Class stu_class;
      //unsigned short int class_sum;
}Class_System;

Class_System class_system;

void Init_Class_System()
{
      memset(&class_system,0,sizeof(Class_System));
      printf("class_system initialization success\n");
}

void Add_A_Student_To_Class_System()
{
      char name ={'\0'};
      unsigned short int age = 0;
      unsigned short int class_number = 0;
      unsigned short int chinese_scores = 0;
      unsigned short int math_scores = 0;
      unsigned short int english_scores = 0;
      unsigned short int number = 0;
      //printf("please input name,age,class_number(1-9),chinese score,math score, english score and the number(1-9)\n");
      printf("input class_number(1-9), stu number(1-9),age, chinese score, math score, english score and name.\n");
      //scanf("%s,%h,%h,%h,%h,%h,%h",name,&age,&class_number,
      //      &chinese_scores,&math_scores,&english_scores,&number);
      scanf("%hd,%hd,%hd,%hd,%hd,%hd,%s", &class_number, &number, &age, &chinese_scores, &math_scores, &english_scores, name);
      printf("class_nu:%d,number:%d, age:%d,chinese:%d,math:%d,english:%d,name:%s.\n",
                              class_number, number, age, chinese_scores, math_scores, english_scores, name);
      //input format: 1,2,3,4,5,6,tom
      strcpy(class_system.stu_class.students.name,name);

      class_system.stu_class.students.age = age;
      class_system.stu_class.students.class_number = class_number;
      class_system.stu_class.students.chinese_scores = chinese_scores;
      class_system.stu_class.students.math_scores = math_scores;
      class_system.stu_class.students.english_scores = english_scores;

      printf("Add student info success!\n");
}

int main()
{
      int i;
      Init_Class_System();
      for(i = 0; i<10;i++)
      {
                Add_A_Student_To_Class_System();
      }

      return 0;
}

进无止进 发表于 2016-09-03 00:33

回复 2# 451006071
谢谢啊,真的是这样,我自己调了很久都没想明白问题在哪儿,还以为是初始化的问题



shang2010 发表于 2016-09-03 11:42

segment fault


一般是没有写过代码的人才会问,{:yxh5:}
页: [1]
查看完整版本: 这个程序运行为什么崩溃