结构体相关知识

#include<iostream>
#include<string>
using namespace std;
//结构体的定义
struct Student
{  
    //成员列表
    string name;
    int age;
    float score;
}s3;

int main()
{
    //结构体变量的创建1;
    struct Student s1;
    s1.name = "张三";
    s1.age = 19;
    s1.score = 93;
    cout << "name:" << s1.name << "age:" << s1.age << "score:" << s1.score << endl;
    //结构体变量的创建2;
    struct Student s2;
    s2 = { "李四",18,90 };
    cout << "name:" << s2.name << "age:" << s2.age << "score:" << s2.score << endl;
    //结构体变量的创建3;
    s3 = { "王五",19,98 };
    cout << "name:" << s3.name << "age:" << s3.age << "score:" << s3.score << endl;

    //结构体数组的创建
    struct Student array[3] =
    {
        {"张三",19,93},
        { "李四",18,90},
        { "王五",19,98}
    };
    //给结构体变量赋值;
    array[1].name = "小明";
    array[1].age = 19;
    array[1].score = 92;

    for (int i = 0; i < 3; i++)
    {
        cout << "name:" << array[i].name << "age:" << array[i].age << "score:" << array[i].score << endl;
    }
    //利用指针进行访问;
    struct Student s4 = { "小红",18,94 };
    struct Student* p = &s4;
    cout << "name:" << p->name << "age:" << p->age << "score:" << p->score << endl;

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值