• 结构体


    结构体

    1、  用户可以自定义的数据类型:结构体类型(struct)、类类型(class)、枚举类型(enumeration)、共用体类型(union)。

    2、  为什么使用结构体:有时需要将不同类型的数据组合成一个有机的整体,方便用户使用

    3、 声明结构体类型的一般形式:

    struct 结构体类型名

    {

    成员表;

    };

    结构体类型名用来做结构体类型的标志,和int,double,char等是一样的;

    在声明结构体是必须对各成员都进行类型的声明,每一个成员也成为结构体中的一个域(field),成员表又叫做域表,声明结构体类型的位置一般在文件的开头,在所有函数之前,也可以在函数中声明结构体:

    类型名 成员名;

    4、 结构体变量的定义方法

    (1)       先声明结构体类型再定义变量

    结构体类型名   结构体变量名;

    例如:Student student1,student2;

    (2)       在声明的同时定义变量

    struct 结构体名

    {

    成员表;

    }变量名;

    例如:

    struct Student

    {

    int num;

    char name[20];

    char sex;

    int age;

    }student1,student2;

    (3)       直接定义结构体类型变量

    struct                //此处没有结构体类型名

    {

    成员表;

    }变量名表;

    5、 结构体应用实例一:定义结构体、结构体变量并赋值,定义结构体变量数组并赋值

    #include<iostream>

    #include<QDebug>

    using namespace std;

    struct Data//定义结构体,只是定义,并没有分配内存

    {

        int m_year;

        int m_month;

        int m_day;

    };

    struct Student//定义结构体并且定义结构体变量student1,student2,student2赋值

    {

       int m_num;

       char m_name[20];

       char m_sex[2];

       int m_age;

       float m_score;

       char m_addr[40];

       Data da1;

    }student2,student1={10002,"wangminghe","f",24,99.9,"chinashan",1994,12,10};

    int main()

    {

        Student student2={102,"wanhe","m",24,99,"china,dong",1994,12,10};

        Student student3=student1;//直接将student1赋值给student2

        student3.m_num=13200;//.是成员运算符,在所有的运算符中优先级最高

        Student stu[3]={{102,"wanhe","m",24,99,"china,dong",1994,12,10},

                        {102,"wanhe","m",24,99,"china,dong",1994,12,10},

                        {102,"wanhe","m",24,99,"china,dong",1994,12,10}};//定义结构体数组并进行初始化

     

        qDebug()<<stu[0].m_name;

        qDebug()<<"student2的地址为:"<<long(&student2);

        qDebug()<<student3.m_num;

    }

    6、 指向结构体变量的指针

    #include<iostream>

    #include<QDebug>

    #include<QString>

    using namespace std;

    struct student

    {

        char m_name1[20];

        int m_age;

        int m_socre;

    };

    int main()

    {

        student stu={"zhangsan",24,100};//定义了一个stu结构体变量

        student *p=&stu;//定义了一个p指针,该指针p指向结构体变量stu的首地址

        qDebug()<<"stu的首地址为:"<<&stu;

        qDebug()<<"p为:"<<p;

        qDebug()<<stu.m_name1<<' '<<stu.m_age<<' '<<stu.m_socre;//结构体变量.成员名

        qDebug()<<(*p).m_name1<<' '<<(*p).m_age<<' '<<(*p).m_socre;//(*p).成员名

        qDebug()<<p->m_name1<<' '<<p->m_age<<' '<<p->m_socre;//p->成员名,“->”称为指向运算符

        //这几个量是等价的

        qDebug()<<"年龄为:"<<p->m_age<<"年龄的地址为:"<<&stu.m_age<<"p的地址为:"<<p;

        //p->m_age得到p指向的结构体变量成员m_age的值

        qDebug()<<"年龄为:"<<p->m_age++<<"年龄的地址为:"<<&stu.m_age<<"p的地址为:"<<p;

        //p->m_age++得到的是P指向的结构体变量成员m_age的值,之后再使m_age加一

        qDebug()<<"年龄为:"<<stu.m_age<<"年龄的地址为:"<<&stu.m_age;

        qDebug()<<"年龄为:"<<++p->m_age<<"年龄的地址为:"<<&stu.m_age<<"p的地址为:"<<p;

        //++p->m_age得到的是p指向结构体变量成员m_age加一之后的值

        qDebug()<<"年龄为:"<<stu.m_age<<"年龄的地址为:"<<&stu.m_age;

       

        return 0;

     

    }

  • 相关阅读:
    liunx某台服务器无法访问其他服务器!!!!!!!!
    下载历史版本CentOS
    通过sparkstreaming分析url的数据
    Linux查看空间大小的命令
    secureCRT背景颜色
    布谷鸟算法详细讲解
    matlab 绘图
    浏览器内存泄露问题
    C#和java的语法区别
    i-m-a-g-e-7
  • 原文地址:https://www.cnblogs.com/wangxiansendebaobaowu/p/8804538.html
Copyright © 2020-2023  润新知