[转]struct 用法深入探索
作者: Cloudward
1. struct的巨大作用
面对一个人的大型C/C++程序时,只看其对struct的使用情况我们就可以对其编写者的编程经验进行评估。因为一个大型的C/C++程序,势必要涉及一些(甚至大量)进行数据组合的结构体,这些结构体可以将原本意义属于一个整体的数据组合在一起。从某种程度上来说,会不会用struct,怎样用struct是区别一个开发人员是否具备丰富开发经历的标志。
{
int a;
char b;
};
{
char a;
short b;
};
{
int a;
char b;
float c;
}
{
int iPacketType; //报文类型标志
union //每次传送的是三种报文中的一种,使用union
{
struct structA packetA;
struct structB packetB;
struct structC packetC;
}
};
Send(char * pSendData, unsigned int iLen);
发送方可以直接进行如下调用发送struct CommuPacket的一个实例sendCommuPacket:
Send( (char *)&sendCommuPacket , sizeof(CommuPacket) );
假设接收函数的原形如下:
// pRecvData:发送字节流的首地址,iLen:要接收的长度
//返回值:实际接收到的字节数
unsigned int Recv(char * pRecvData, unsigned int iLen);
{
case PACKET_A:
… //A类报文处理
break;
case PACKET_B:
… //B类报文处理
break;
case PACKET_C:
… //C类报文处理
break;
}
Recv( (char *)&recvCommuPacket , sizeof(CommuPacket) );
Intel、微软等公司曾经出过一道类似的面试题:
3. struct example1
4. {
5. short a;
6. long b;
7. };
9. {
10. char c;
11. example1 struct1;
12. short e;
13. };
14. #pragma pack()
16. {
17. example2 struct2;
19. cout << sizeof(example2) << endl;
20. cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2)
<< endl;
22. }
16
4
{
char a;
short b;
char c;
};
{
char a;
int b;
char c;
};
· 使用伪指令#pragma pack (),取消自定义字节对齐方式。
struct naturalalign
{
char a;
int b;
char c;
};
#pragma pack ()
在C++语言中struct具有了“类” 的功能,其与关键字class的区别在于struct中成员变量和函数的默认访问权限为public,而class的为private。
{
char a;
…
}
class classB
{
char a;
…
}
a.a = 'a'; //访问public成员,合法
classB b;
b.a = 'a'; //访问private成员,不合法
struct structA
{
char a;
char b;
int c;
};
structA a = {'a' , 'a' ,1}; // 定义时直接赋初值
看看下面的程序:
3. {
4. int iMember;
5. char *cMember;
6. };
8. {
9. structA instant1,instant2;
10.char c = 'a';
11. instant1.iMember = 1;
12. instant1.cMember = &c;
13.instant2 = instant1;
14.cout << *(instant1.cMember) << endl;
15.*(instant2.cMember) = 'b';
16. cout << *(instant1.cMember) << endl;
17. return 0;
}
16行的输出结果是:b
struct与class的比较
[ 2005-11-02 13:13:55 | 作者: Cloudward ]
C#
{
string Name;
int height;
int weight
{
string Name;
int height;
int weight
public bool overWeight()
{
//implement something
}
}
{
int hours;
int minutes;
int seconds;
public void passtime()
{
//implementation of behavior
}
}
{
public static ovid Main
{
Person Myperson=new Person //声明结构
TestTime Mytime=New TestTime //声明类
}
}
类:完全可扩展的,除非显示的声明sealed 否则类可以继承其他类和接口,自身也能被继承
interface IImage
{
void Paint();
}
struct Picture : IImage
{
public void Paint()
{
// painting code goes here
}
private int x, y, z; // other struct members
}
{
char a;
…
}
class classB
{
char a;
…
}
a.a = 'a'; //访问public成员,合法
classB b;
b.a = 'a'; //访问private成员,不合法
struct structA
{
char a;
char b;
int c;
};
structA a = {'a' , 'a' ,1}; // 定义时直接赋初值
[转]struct 用法深入探索的更多相关文章
- union内嵌struct用法
// union内嵌struct用法 // 众所周知,union为联合体,struct为结构体.下面根据实例谈谈用法 #include <stdio.h> #include & ...
- Learn day9 粘包\struct用法\hashlib校验\socketserver并发\模块引入\进程\join\守护进程
1.粘包现象 总结 : 导致黏包现象的两种情况 hello,worl d (1) 在发送端,发送数据太快,频繁发送 (2) 在接收端,接收数据太慢,延迟截取 # ### 服务端 import sock ...
- C/C++语法知识:typedef struct 用法详解
第一篇:typedef struct与struct的区别 1. 基本解释 typedef为C语言的关键字,作用是为一种数据类型定义一个新名字.这里的数据类型包括内部数据类型(int,char等)和自定 ...
- 结构体定义 typedef struct 用法详解和用法小结
typedef是类型定义的意思.typedef struct 是为了使用这个结构体方便.具体区别在于:若struct node {}这样来定义结构体的话.在申请node 的变量时,需要这样写,stru ...
- 05typedef struct用法详解与小结
1.基本解释 typedef为C语言的关键字,作用是为一种数据类型定义一个新名字,这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等). 在编程中使用typedef ...
- typedef struct用法详解与小结
1.基本解释 typedef为C语言的关键字,作用是为一种数据类型定义一个新名字,这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等). 在编程中使用typedef ...
- struct和typedef struct用法
参考:http://www.cnblogs.com/qyaizs/articles/2039101.html C语言: typedef struct Student{ int score; }Stu; ...
- python 中 struct 用法
下面就介绍这个模块中的几个方法. struct.pack():我的理解是,python利用 struct模块将字符(比如说 int,long ,unsized int 等)拆成 字节流(用十六进制表示 ...
- C结构体struct用法小结
结构体和int,float等类型一样是一种常用的类型,它是由各种基本数据类型构成,通常包含有struct关键字,结构体名,结构体成员,结构体变量. 一.结构体定义 通常有3种定义方式,以例子方式表示: ...
随机推荐
- SET STATISTICS IO和SET STATISTICS TIME 在SQL Server查询性能优化中的作用
近段时间以来,一直在探究SQL Server查询性能的问题,当然也漫无目的的查找了很多资料,也从网上的大神们的文章中学到了很多,在这里,向各位大神致敬.正是受大神们无私奉献精神的影响,所以小弟也作为回 ...
- 回滚Swtichover
从11.2.0.2开始,如果由于某种原因switchover没有成功,可以回滚switchover. For physical standby databases in situations wher ...
- NSString 遍历
遍历NSString网上大多数有两种方法 最简单有效的是: NSString *name=[[NSString alloc] initWithFormat:@"小猫咪爱上大老鼠!!" ...
- Android 抓包,监控流量工具之 mitmproxy
转:http://greenrobot.me/devpost/how-to-debug-android-http-get-started/ mitmproxy实践教程之调试 Android 上 HTT ...
- ID卡常见型号
EM ID卡,主要是采用瑞士EM或台湾GK公司的4100.4102系列IC芯片 + 线圈 + 卡基封装而成. (1)4001感应式ID厚卡:台湾4001 COB 特征:普通型感应卡,厚薄适中,带有ID ...
- Mozilla推荐的CSS书写顺序
//显示属性displaylist-stylepositionfloatclear //自身属性widthheightmarginpaddingborderbackground //文本属性color ...
- 远程监控 – 应用程序运行状况测量 CSF 博客
在远程监控基础知识和故障排除中,我们探讨了 Windows Azure 平台提供的基础指标.信息源.工具和脚本,介绍了有关监控和应用程序运行状况的基本原则.我们演示了如何利用这些基本原则对在 Wind ...
- Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答
Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答 Boost正则表达式库regex常用search和match示例 发表回复 Boo ...
- Hibernate 1、Hello Hibernate
所使用到的jar 包: 1.创建实体类 public class User { private Integer id; private String name; private String addr ...
- AndroidUI 布局动画-点九PNG技术
下面是正常情况与使用点9切图以后的效果对比: <Button android:id="@+id/button1" android:layout_width="fil ...