职业养老选手 2022-05-26 15:42 采纳率: 100%
浏览 41
已结题

C语言结构体的定义和初始化

定义一个学生结构体,数据成员包括学号,姓名,性别,生日,然后输出三个同学的数据
(1001011 王丽 F 1995/1012
1001012 李军 M 1995/5/24
1001013 赵斌 M 1994/2/25
)

  • 写回答

1条回答 默认 最新

  • 关注
    #include <iostream>
    #include <stdio.h>
    #include <vector>
    
    using namespace std;
    
    
    struct Student
    {
        int id;
        char* name;
        char* sex;
        char* birthday;
    
    };
    
    int main()
    {
        vector<Student> res;
    
        Student stu1;
        stu1.id=10001;
        stu1.name="王丽";
        stu1.sex="F";
        stu1.birthday="1995/10/12";
    
        Student stu2;
        stu2.id=10002;
        stu2.name="李军";
        stu2.sex="F";
        stu2.birthday="1995/5/24";
    
        Student stu3;
        stu3.id=10003;
        stu3.name="赵斌";
        stu3.sex="F";
        stu3.birthday="1995/2/25";
    
        res.push_back(stu1);
        res.push_back(stu2);
        res.push_back(stu3);
    
        for(int i=0;i<res.size();++i)
        {
            printf("%d %s %s %s\n",res[i].id,res[i].name,res[i].sex,res[i].birthday);
        }
    
        return 0;
    }
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月11日
  • 已采纳回答 6月3日
  • 创建了问题 5月26日