struct基础

/*
 * structBase.cpp
 *
 *  Created on: 2016年8月1日
 *      Author: zroad
 */
#include<iostream>
#include<cstring>
using namespace std;



int main() {
    //一、声明结构体:
    //结构体:由一系列具有相同类型或不同类型的数据构成的数据集合,也叫结构。
    struct Student {
        int num;
        char name[20];
        char sex;
        int age;
        float score;
        char addr[30];
    };

    //二、结构体变量的定义:
    //1、声明后再定义结构体变量,最常用
    Student stu2;

    //2、声明结构体时定义同时定义变量
    /*
     * struct Student {
        int num;
        char name[20];
        char sex;
        int age;
        float score;
        char addr[30];
    }stu1,stu2;
     */

    //3、直接定义结构体变量:
    /*
     * struct {
        int num;
        char name[20];
        char sex;
        int age;
        float score;
        char addr[30];
    }stu1,stu2;
     */

    //cout << sizeof(stu1) << endl;
    cout << sizeof(stu2) << endl;

    //三、结构体变量初始化:
    //cout << stu1.num <<" " << stu1.name << " " << stu1.age << endl;
    //1、定义变量后,再初始化(常用)
    Student stu1 = {
            1000,
            "zroad",
            'm',
            30,
            100,
           "guangzhou"
    };

    cout << stu1.num <<" " << stu1.name << " " << stu1.age << endl;

    //2、声明结构时初始化
    struct Date {
        int month;
        int day;
        int year;
    } birth = {
        12,
        31,
        2004
    };


    struct StudentNew {
        int num;
        Date birthday; //结构体可以嵌套使用
        char name[20];
        char sex;
        int age;
        float score;
        char addr[30];
    } student2 = {
            10002,
            birth,
            "zhangrunjia",
            'm',
            2,
            99,
            "guangzhou"
    };

    //四、结构体的引用
    cout << student2.birthday.year << " " << student2.num << " " << student2.addr << endl;


    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值