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{.. ...
随机推荐
- Android 防破解技术简介
Android 防破解技术简介 这几年随着互联网的不断发展,Android App 也越来越多!但是随之而来的问题也越来越多,这其中比较令人头疼的问题就是:有些不法分子利用反编译技术破解 App,修改 ...
- unicode转换中文
<!doctype html><html lang="en"> <head> <meta http-equiv="Refres ...
- 160523、Oracle建立表空间和用户
建立表空间和用户的步骤: 用户 建立:create user 用户名 identified by "密码"; 授权:grant create session to 用户名; gra ...
- PhoneGap在Mac环境下使用问题小结
PhoneGap在Mac环境下使用问题小结1,问题一:在执行命令phonegap run ios出现“Cordova needs ios-sim version 1.7 or greater, you ...
- Servlet------>jsp自定义标签(JSPTAG接口)
TagSupport实现类里不只实现了tag接口,还有tag接口的子接口,也就是IterationTag子接口中增加了doAfterBody()方法和EVAL_BODY_AGAIN常量,为了实现标签体 ...
- Oracle Schema Objects(Schema Object Storage And Type)
One characteristic of an RDBMS is the independence of physical data storage from logical data struct ...
- hadoop运维问题记录
hadoop综合问题记录 1.hdfs无法启动 错误: 2018-04-25 14:36:09,293 - Retrying after 10 seconds. Reason: Execution o ...
- php composer,update-ca-trust
安装 ComposerComposer 需要 PHP 5.3.2+ 才能运行. $ curl -sS https://getcomposer.org/installer | php这个命令会将 com ...
- 用linux c求最大公约数
我写了两中函数,一个是辗转相除法一个是更相减损法,主要代码如下: /*辗转相除法*/int gcd(int a, int b) { ) { return b; } else { return gcd( ...
- [已解决]centos6.4 php连接mysql和memcache提示权限不允许
昨天新安装了64位系统,所以就重新安装了一个centos的虚拟机,配置了一个测试环境,刚开始是页面打不开,这个确定是selinux规则没有设置好使用如下命令解决: chcon -R -h -t htt ...