C++中的结构体

以下为本人在大一时阅读《C++ Primer Plus》中关于结构体的章节所做的笔记

 

结构体

一个结构体的成员可以生命在另一个结构体中
(Member of struct can be another defined struct )

struct  date
{  
    int  day ;
    int  year ;
} ;
struct employee
{  
    date  birthday ; //上一个结构体的内容
    long  code ;
    char  phone [ 11 ] ;
}  worker1, worker2 ;

注意:
不可递归结构(Infinite recursion structure)

struct  person
{  char  name [ 10 ] ;
    long  code ;
    double  salary ;
    char address [ 50 ] ;
    char  phone [ 11 ] ; 
    person * son; //Error!不可递归结构
}  worker1, worker2 ;

结构体的初始化:

struct employee
{  char  name [ 10 ] ;
    long  code ;
    double  salary ;
    char address [ 50 ] ;
    char  phone [ 11 ] ;
}  
employee worker = {"Wang Li " , 991083456, 1200.5, "guang zhou " , " 87111111 " } ;

两种访问结构体变量的方式:

1.通过变量名:

struct student
{
        int num;
};
student stu1;
stu1.num=1;

2.通过指针

struct student
{
        int num;
};
student *stu1;
stu1->num=1;

相同类型的结构体变量可以互相赋值,即是将stu1中所有的内容拷贝到stu2中

注意:

struct student1
{
        int num;
};
struct student2
{
        int num;
};
student1 stu1;
student2 stu2;

这时候stu1和stu2不是同一个结构体类型

结构体数组:

struct student1
{
        int num;
}stu[10];

结构体排序:
问题:结构变量的总体交换降低了排序的效率。
(the overall exchange of struct variables reduces the efficiency of sorting.)
解决方法:使用索引机制,建立结构指针数组
(using the index mechanism, the establishment of a struct pointer array)
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值