C/C++结构体总结
| 1 | #include"iostream" |
| 2 | using namespace std; |
| 3 | |
| 4 | struct TestStruct |
| 5 | { |
| 6 | int iNumber; |
| 7 | ]; |
| 8 | char ch; |
| 9 | double dNumber; |
| 10 | }; |
| 11 | //【嵌套的结构体类型成员】 |
| 12 | struct Date |
| 13 | { |
| 14 | int Day; |
| 15 | int Month; |
| 16 | int Year; |
| 17 | } ; |
| 18 | struct Person /*定义结构体*/ |
| 19 | { |
| 20 | ]; |
| 21 | int Age; |
| 22 | struct Date Birthday; //嵌套Date结构体类型成员 |
| 23 | } ; |
| 24 | //【结构体中的指针成员】 |
| 25 | struct Student{ |
| 26 | char *name; |
| 27 | int score; |
| 28 | struct Student* next; |
| 29 | }; |
| 30 | |
| 31 | int main(int argc, char* argv[]) |
| 32 | { |
| 33 | //结构体的大小【结构体对齐】 |
| 34 | //A: 结构体变量的首地址能够被其最宽基本类型成员的大小所整除 |
| 35 | //B: 结构体每个成员相对于结构体首地址的偏移量都是成员自身大小的整数倍,如有需要编译器会在成员之间加上填充字节 |
| 36 | //C:结构体的总大小为结构体最宽基本类型成员大小的整数倍 |
| 37 | cout<<"Size of TestStruct "<<sizeof(TestStruct)<<endl; |
| 38 | cout<<"Size of Date "<<sizeof(Date)<<endl; |
| 39 | cout<<"Size of Person "<<sizeof(Person)<<endl; |
| 40 | |
| 41 | //【结构体变量】 |
| 42 | }; |
| 43 | cout<<"TestStruct ch:"<<t.ch<<" charArray:"<<t.charArray<<" dNumber:"<<t.dNumber<<" iNumber:"<<t.iNumber<<endl; |
| 44 | |
| 45 | //【结构体数组】 |
| 46 | }}; |
| 47 | ;i<3;i++) |
| 48 | { |
| 49 | cout<<"TestStruct["<<i<< "]ch:"<<tt[i].ch<<" charArray:"<<tt[i].charArray<<" dNumber:"<<tt[i].dNumber<<" iNumber:"<<tt[i].iNumber<<endl; |
| 50 | } |
| 51 | //【指针变量与结构体变量】 |
| 52 | //必须要给结构体指针变量赋予一个有效的结构体变量地址,才能正常操作结构体指针变量。 |
| 53 | //TestStruct *p =&t;否则将出现不可预知的问题。(*p).ch 等价于 p->ch |
| 54 | TestStruct *p =&t; |
| 55 | cout<<"TestStruct p->ch:"<<p->ch<<" p->charArray:"<<p->charArray<<" p->dNumber:"<<p->dNumber<<" p->iNumber:"<<p->iNumber<<endl; |
| 56 | //【指向结构体数组的指针】 |
| 57 | //pp的初值为tt,即指向第一个元素,则pp加1后pp就指向下一个元素的起始地址(即tt[1]得起始地址)。 |
| 58 | TestStruct *pp ; |
| 59 | ;pp++) |
| 60 | { |
| 61 | cout<<"TestStruct pp->ch:"<<pp->ch<<" pp->charArray:"<<pp->charArray<<" pp->dNumber:"<<pp->dNumber<<" pp->iNumber:"<<pp->iNumber<<endl; |
| 62 | } |
| 63 | //【嵌套的结构体类型成员】 |
| 64 | //访问嵌套结构体Date的成员 per->Birthday.Year 等价于 (*per).Birthday.Year |
| 65 | Person *per; |
| 66 | per = new Person; //per=(Person *)malloc(sizeof(Person)); |
| 67 | cout<<"Input name,age,year,month,day"<<endl; |
| 68 | cin>>per->Name>>per->Age>>per->Birthday.Year>>per->Birthday.Month>>per->Birthday.Day; |
| 69 | cout<<"Name:"<<per->Name<<" Age:"<<per->Age<<" Birthday:"<<(*per).Birthday.Year<<"/"<<per->Birthday.Month<<"/"<<per->Birthday.Day<<endl; |
| 70 | delete per; //free(per); |
| 71 | //【结构体中的指针成员】 |
| 72 | Student stu,*Pstu; |
| 73 | //结构体成员指针需要初始化 |
| 74 | stu.name = new char;//stu.name = (char*)malloc(sizeof(char)); |
| 75 | strcpy(stu.name,"ddd"); |
| 76 | ; |
| 77 | //结构体指针需要初始化 |
| 78 | Pstu = new Student;//Pstu = (struct Student*)malloc(sizeof(struct Student)); |
| 79 | //构体指针的成员指针同样需要初始化 |
| 80 | Pstu->name = new char;//Pstu->name = (char*)malloc(sizeof(char)); |
| 81 | stu.next = Pstu; |
| 82 | strcpy(Pstu->name,"cccc"); |
| 83 | ; |
| 84 | Pstu->next = NULL; |
| 85 | cout<<"stu.name: "<<stu.name<<" stu.score: "<<stu.score<<endl; |
| 86 | cout<<"stu.next->name: "<<stu.next->name<<" stu.next->score: "<<stu.next->score<<endl; |
| 87 | cout<<"Pstu->name: "<<Pstu->name<<" Pstu->score: "<<Pstu->score<<endl; |
| 88 | delete Pstu; |
| 89 | |
| 90 | ; |
| 91 | |
| 92 | } |
| 93 |
C/C++结构体总结的更多相关文章
- Go结构体实现类似成员函数机制
Go语言结构体成员能否是函数,从而实现类似类的成员函数的机制呢?答案是肯定的. package main import "fmt" type stru struct { testf ...
- C#基础回顾(二)—页面值传递、重载与重写、类与结构体、装箱与拆箱
一.前言 -孤独的路上有梦想作伴,乘风破浪- 二.页面值传递 (1)C#各页面之间可以进行数据的交换和传递,页面之间可根据获取的数据,进行各自的操作(跳转.计算等操作).为了实现多种方式的数据传递,C ...
- go语言结构体
定义: 是一种聚合的数据类型,是由零个或多个任意类型的值聚合成的实体. 成员: 每个值称为结构体的成员. 示例: 用结构体的经典案例处理公司的员工信息,每个员工信息包含一个唯一的员工编号.员工的名字. ...
- C语言中的结构体
用户自己建立自己的结构体类型 1. 定义和使用结构体变量 (1).结构体的定义 C语言允许用户自己建立由不同类型数据组成的组合型的数据结构,它称为结构体. (2).声明一个结构体类型的一般形式为: ...
- C++_系列自学课程_第_12_课_结构体
#include <iostream> #include <string> using namespace std; struct CDAccount { double bal ...
- java socket传送一个结构体给用C++编写的服务器解析的问题
另一端是Java写客户端程序,两者之间需要通信.c++/c接收和发送的都是结构体,而Java是直接发送的字节流或者byte 数组.解决方法:c++/c socket 在发送结构体的时候其实发送的也是字 ...
- swift学习笔记3——类、结构体、枚举
之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- C语言结构体对齐
1.结构体变量中的元素如何访问? (1)数组中元素的访问方式:表面上有2种方式(数组下标方式和指针方式):实质上都是指针方式访问.(2)结构体变量中的元素访问方式:只有一种,用.或者->的方式来 ...
- C与指针(结构体指针,函数指针,数组指针,指针数组)定义与使用
类型 普通指针 指针数组(非指针类型) 数组指针 结构体指针 函数指针 二重指针 定义方式 int *p; int *p[5]; int (*p)[5]; int a[3][5]; struct{.. ...
随机推荐
- Erlang语言学习入门
这是一个命令行程序,可以直接在里面输入表达式进行计算,例如来一个简单的: Erlang R15B01 (erts-5.9.1) [smp:4:4] [async-threads:0] Eshell V ...
- js修改剪切板内容的方法
代码如下: //绑定在了body上,也可以绑定在其他可用元素行,但是不是所有元素都支持copy事件. $(document.body).bind({ copy: function(e) {//copy ...
- 666:放苹果(划分dp)
666:放苹果 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示 ...
- 【BZOJ3120】Line 矩阵乘法
[BZOJ3120]Line Description Wayne喜欢排队……不对,是Wayne所在学校的校长喜欢看大家排队,尤其是在操场上站方阵.某日课间操时,校长童心大发想了一个极具观赏性的列队方案 ...
- Arduino开发版学习计划--小车的行走
小车的前进后退,左右转弯 代码如下 void motor(char pin,char pwmpin,char state,int val) { pinMode(pin, OUTPUT); ) { an ...
- mysql支持跨表删除多条记录
在Mysql4.0之后,mysql开始支持跨表delete. Mysql可以在一个sql语句中同时删除多表记录,也可以根据多个表之间的关系来删除某一个表中的记录. 假定我们有两张表:Product表和 ...
- Maven结构下 properties 读取
Properties properties = new Properties();InputStream in = ClassLoader.class.getResourceAsStream(&quo ...
- 160526、高并发之LVS搭建负载均衡
LVS介绍: LVS的英文全称是Linux Virtual Server,即Linux虚拟服务器.它是我们国家的章文嵩博士的一个开源项目.在linux内核2.6中,它已经成为内核的一部分,在此之前的内 ...
- hibernate的二级缓存----collection和query的二级缓存
collection二级缓存: 不使用集合的二级缓存时: 运行下面的代码: @Test public void testCollectionSecondLevelCache1(){ Departmen ...
- ionic 移动开发性能调优-去除动画
<ion-refresher></ion-refresher> ion-refresher指令有以下可选的属性: on-refresh - 当用户向下拉动足够的距离并松开时,执 ...