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{.. ...
随机推荐
- Popwindow系列
一系列干货等你来拿 关于我:http://www.cnblogs.com/dubo-/ 项目中也有很多实用的事例,等待录入中...
- $("#btn").click(function(){ });只有在页面加载的时候才会有效触发
例: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title& ...
- URL地址传值型多条件搜索JS
function ResetSearchVal(objArray) { var strUrl = location.href; ; i < objArray.length; i++) { var ...
- java基础---->hashSet的简单分析(一)
对于HashSet而言,它是基于HashMap实现的,底层采用HashMap来保存元素的.今天我们就简单的分析一下它的实现.人生,总会有不期而遇的温暖,和生生不息的希望. HashSet的简单分析 一 ...
- 简介Objective-C语言
2011-05-11 11:20 佚名 百度百科 字号:T | T Objective-C,是扩充C的面向对象编程语言.主要使用于Mac OS X和GNUstep这两个使用OpenStep标准的系统, ...
- registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped.
最近在用maven整合SSH做个人主页时候,在eclipse里面使用tomcat7插件发布项目是没有问题的,但当打包成war之后,使用tomcat7单独发布项目,就出现了以下的错误. 严重: Cont ...
- dstat命令--检查linux系统性能
系统性能的相关工具:vmstat.netstat.iostat.ifstat. dstat命令可以查看虚拟内存.网络连接和接口.cpu活动.输入/输出设备等.
- 01. Java序列化与反序列化简介
Java对象的序列化与反序列化 ; 给大家讲解一下什么是序列化 & 反序列化 当两个进程进行远程通讯的时候,彼此相互可以发送各种类型的数据,如文本,图片,语音和视频等无论是任何类型,最终都会 ...
- python中读取json文件报错,TypeError:the Json object must be str, bytes or bytearray,not ‘TextIOWrapper’
利用python中的json读取json文件时,因为错误使用了相应的方法导致报错:TypeError:the Json object must be str, bytes or bytearray,n ...
- python和shell之间变量的相互调用
python -> shell: 1.环境变量 2.字符串连接 3.通过管道 import os var=’123’ os.popen(’wc -c’, ’w’).write(var) 4.通过 ...