struct和typedef struct的用法

        我首先想到的去MSDN上看看sturct到底是什么东西,虽然平时都在用,但是每次用的时候都搞不清楚到底这两个东西有什么区别,既然微软有MSDN,我们为什么不好好利用呢,下面是摘自MSDN中的一段话:

 

The struct keyword defines a structure type and/or a variable of a structure type. A structure type is a user-defined composite type. It is composed of "fields" or "members" that can have different types.In C++, a structure is the same as a class except that its members are public by default.In C, you must explicitly use the struct keyword to declare a structure. In C++, this is unnecessary once the type has been defined.You have the option of declaring variables when the structure type is defined by placing one or more comma-separated variable names between the closing brace and the semicolon.

 

从上面这段话中我们知道在c++中struct和typedef struct的用法基本没有区别了。下面是代码:

 

 

 

下面介绍在c语言中的用法

 typedef与结构结合使用:

这语句实际上完成两个操作:

1) 定义一个新的结构类型

struct tagMyStruct
{
 int iNum;
 long lLength;
};

        分析:tagMyStruct称为“tag”,即“标签”,实际上是一个临时名字,struct 关键字和tagMyStruct一起,构成了这个结构类型,不论是否有typedef,这个结构都存在。

        我们可以用struct tagMyStruct varName来定义变量,但要注意,使用tagMyStruct varName来定义变量是不对的,因为struct 和tagMyStruct合在一起才能表示一个结构类型。

2) typedef为这个新的结构起了一个名字,叫MyStruct。

typedef struct tagMyStruct MyStruct;

  因此,MyStruct实际上相当于struct tagMyStruct,我们可以使用MyStruct varName来定义变量

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值