超级熊宝宝 发表于 2018-4-29 11:52:09

有没有大神,解决一个简单结构体问题

新手刚学到结构体,用VC6.0写了个简单小程序,记录学生基本信息。debug不出问题但运行出错,大神帮忙看一下好不好啊!

#include<stdio.h>

void main()
{
    struct date
    {
      int month;
      int day;
      int year;
    };

    struct student
    {
      int num;
      char name;
      char sex;
      struct date birthday;
      double score;
    }boy1, boy2;

    printf("please input number: ");
    scanf("%d", &boy1.num);
    printf("please input name: ");
    scanf("%s", &boy1.name);
    printf("please input sex: ");
    scanf("%s", &boy1.sex);
    printf("please input score: ");
    scanf("%lf", &boy1.score);
    printf("please input birthday(MM): ");
    scanf("%d", &boy1.birthday.month);
    printf("please input birthday(DD): ");
    scanf("%d", &boy1.birthday.day);
    printf("please input birthday(YY): ");
    scanf("%d", &boy1.birthday.year);

    boy2 = boy1;

    printf("Student Number = %d\nStudent Name = %s\nStudent Sex = %s\n", boy1.num, boy1.name, boy1.sex);
    printf("Student Birthday = %d-%d-%d\nStudent Score = %lf\n\n", boy1.birthday.year, boy1.birthday.month, boy1.birthday.day, boy1.score);
    printf("Student Number = %d\nStudent Name = %s\nStudent Sex = %s\n", boy2.num, boy2.name, boy2.sex);
    printf("Student Birthday = %d-%d-%d\nStudent Score = %lf\n\n", boy2.birthday.year, boy2.birthday.month, boy2.birthday.day, boy2.score);

}

ba21 发表于 2018-4-29 12:39:58

struct student
{
    int num;
    char name;
    char sex;
    struct date birthday;
    double score;
}boy1, boy2;

超级熊宝宝 发表于 2018-4-29 13:02:03

ba21 发表于 2018-4-29 12:39
struct student
{
    int num;


谢谢你,又是你帮我解答的。
这里想多问一句,是不是字符串必须赋值给数组,不能直接赋值?(基础学得不太好)

ba21 发表于 2018-4-29 14:12:17

超级熊宝宝 发表于 2018-4-29 13:02
谢谢你,又是你帮我解答的。
这里想多问一句,是不是字符串必须赋值给数组,不能直接赋值?(基础学得不 ...

c没有字符串的概论,只有char
字符串用char数组表示
页: [1]
查看完整版本: 有没有大神,解决一个简单结构体问题