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{.. ...
随机推荐
- IOS7 新特性(针对同样讨厌更新后IOS7的开发者)
本文转载至 http://blog.csdn.net/hanbing861210/article/details/13614211 您还未登录!|登录|注册|帮助 首页 业界 移动 云计算 研发 论坛 ...
- iOS-多线程的底层实现
(1)首先回答什么是线程 1个进程要想执行任务,必须得有线程.线程是进程的基本执行单元,一个进程(程序)的所有任务都在线程中执行 (2)什么是多线程 1个进程中可以开启多条线程,每条线程可以并行(同时 ...
- Git 安装与使用
http://blog.csdn.net/lishuo_os_ds/article/details/8078475#sec-1.8.2 http://blog.csdn.net/showhilllee ...
- Win7下搭建安卓android开发环境
本文出自 “孤狼” 博客,请务必保留此出处http://332374363.blog.51cto.com/5262696/1310882 另外,在搭建android开发环境时,还参考了http://w ...
- poj_3159 最短路
题目大意 有N个孩子(N<=3000)分糖果.有M个关系(M<=150,000).每个关系形如:A B C 表示第B个学生比第A个学生多分到的糖果数目,不能超过C.求第N个学生最多比第1个 ...
- 转载hiberinate的懒加载
Hibernate的强大之处之一是懒加载功能,可以有效的降低数据库访问次数和内存使用量.但用的不好就会出现org.hibernate.LazyInitializationException. 这个异常 ...
- 具备双向通行能力的架构对于移动APP属于刚性需求。 WebSocket连接 注册信令
双向通信使用指南_用户指南(开放 API)_API 网关-阿里云 https://help.aliyun.com/document_detail/66031.html 流程描述 (1) 客户端在启动的 ...
- android开发笔记(一)Android studio 输入法
以前都是用的时候查资料做些增添即可,现在下决心系统学习下. 首先发现developer.Android.com在开发工具上开始推出了 Android Studio了,不过他自己没有sdk manage ...
- 4.1 - FTP文件上传下载
题目:开发一个支持多用户同时在线的FTP程序要求:1.用户加密认证2.允许同时多用户登录3.每个用户有自己的家目录,且只能访问自己的家目录4.对用户进行磁盘配额,每个用户的可用空间不同5.允许用户在f ...
- squee_spoon and his Cube VI---郑大校赛(求最长子串)
市面上最常见的魔方,是三阶魔方,英文名为Rubik's Cube,以魔方的发明者鲁比克教授的名字命名.另外,二阶魔方叫Pocket Cube,它只有2*2*2个角块,通常也就比较小:四阶魔方叫Reve ...