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{.. ...
随机推荐
- 13个非常实用的JavaScript小技巧
使用!!操作符转换布尔值 有时候我们需要对一个变量查检其是否存在或者检查值是否有一个有效值,如果存在就返回true值.为了做这样的验证,我们可以使用!!操作符来实现是非常的方便与简单.对于变量可以使用 ...
- ios常用第三方库git下载地址
本文转载至 http://blog.csdn.net/cerastes/article/details/38348599 iOS第三方库下载常用git 1.FMDB https://github.co ...
- iOS 引导页面启动一次
#import "AppDelegate.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)a ...
- android签名,制作key
签名具体步骤: Apk签名首先要有一个keystore的签名用的文件. keystore是由jdk自带的工具keytool生成的.具体生成方式参考一下: 开始->运行->cmd->c ...
- spring boot 加载jsp
1.spring boot启动类继承SpringBootServletInitializer ,并且重写configure方法 package com.springapp.mvc;import jav ...
- swift中类与结构体
写了12个Person来复习,不过完成同样的代码需要敲键盘的次数相比OC确实少了很多,这很多应该归功于Swift中不写分号,以及少了OC中的中括号. 一.类与结构体 两者在Swift中差不了多少了 类 ...
- 修改NavigationBar的分根线颜色
[self.navigationController.navigationBar setShadowImage:[Static ColorToImage:[Static colorWithHexStr ...
- 购物车 cookie session
0-服务器识别用户的目的:服务器存有不同用户的信息,而对这些信息,服务器自身.网站开发管理者.网站访问者会对其读写: 1-暂且存入服务器数据库,购物车分为2种表:购物车入车表和购物车下单表: 2-单个 ...
- initialize myObject by calling a function that returns an object literal
w作用域控制变量的可见范围. JavaScript: The Good Parts Instead of initializing myObject with an object literal, w ...
- python基础-第二篇-基本数据类型
一.运算符 1.算数运算: 算数运算符相信大家都不陌生吧,尤其是加减乘除,好!那我就带着大家看看最后三个,这三个到底是干什么玩意的? %,取两数相除的余数,看图: **,x的多少次幂,看图: //,取 ...