创建结构体变量的三种方法

#include <iostream>
#include "string"
#include "bubbleSort.h"

using namespace std;

//创建学生的数据类型
struct Student {
    //成员列表
    string name;  //姓名
    int age;    //年龄
};

int main() {

    //创建方式一:先创建,再赋值
    struct Student student1;
    student1.name = "张三";
    student1.age = 10;
    cout << "姓名:" << student1.name << "\t年龄:" << student1.age << endl;

    //创建方式二:直接赋值
    struct Student student2 = {"李四", 18};
    cout << "姓名:" << student2.name << "\t年龄:" << student2.age << endl;

    //创建方式三,声明时赋值
    struct Person {
        string name;
        int age;
    } person;
    person.name = "王五";
    person.age = 20;
    cout << "姓名:" << person.name << "\t年龄:" << person.age << endl;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值