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 structkeyword 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的用法基本没有区别了。下面是代码:
#include<iostream>
using namespace std; struct PERSON /* Declare PERSON struct type */
{
int age; /* Declare member types */
long ss;
float weight;
char name[];
} family_member; /* Define object of type PERSON,定义一个PERSON类型的对象family_member,定义对象并不占用内存。*/ /* Structure variables can be initialized. The initialization for each variable must be enclosed in braces. */
/* 这表示在C++中struct其实就是一个类,只不过这个类中的默认访问属性是public类型的,而在c++中默认访问类型是private的 */ struct POINT /* Declare POINT structure */
{
int x; /* Define members x and y */
int y;
} spot = { , }; /* Variable spot has values x = 20, y = 40,定义对象后马上初始化,这时候分配内存 */ struct POINT there; /* Variable there has POINT type */ int main()
{ struct PERSON sister; /* C style structure declaration,C语言风格第一一个PERSON类型的对象sister */
PERSON brother; /* C++ style structure declaration,而在C++中我们已经没有必要加上关键字struct了,
直接用结构体类型PERSON定义对象brother */ sister.age = ; /* assign values to members */
brother.age = ;
family_member.age=; /* 在创建结构体的时候就定义了的对象 */
there.x=spot.x;
POINT p;
p.y=spot.y; cout<<sister.age<<" "<<brother.age<<endl;
cout<<family_member.age<<endl;
cout<<spot.x<<" "<<spot.y<<endl;
cout<<there.x<<" "<<p.y<<endl; return ;
}
下面介绍在c语言中的用法
typedef与结构结合使用:
typedef struct tagMyStruct
{
int iNum;
long lLength;
} MyStruct;
这语句实际上完成两个操作:
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来定义变量。
备注 :本文章转自于:http://blog.csdn.net/xw13106209/article/details/4967689 ,在此感谢他的精彩分享!
struct和typedef struct的用法的更多相关文章
- [转载]彻底弄清struct和typedef struct
struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...
- struct和typedef struct彻底明白了
struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...
- struct和typedef struct
转自:http://www.cnblogs.com/qyaizs/articles/2039101.html struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++ ...
- 细说 struct和typedef struct
细说 struct和typedef struct 参考原文:http://www.cnblogs.com/qyaizs/articles/2039101.html,有些小改动~ 1 首先://注意在C ...
- C++/C中的struct和typedef struct用法和区别
struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...
- struct和typedef struct用法
参考:http://www.cnblogs.com/qyaizs/articles/2039101.html C语言: typedef struct Student{ int score; }Stu; ...
- struct和typedef struct在c++中的用法
#include<iostream> using namespace std; struct test{ int a; }test; //定义了结构体类型test,声明变量时候直接test ...
- struct和typedef struct在c语言中的用法
在c语言中,定义一个结构体要用typedef ,例如下面的示例代码,Stack sq:中的Stack就是struct Stack的别名. 如果没有用到typedef,例如定义 struct test1 ...
- struct 和typedef struct的区别
和int char一样struct也是一种数据类型,也可以声明变量--结构变量. 定义结构体变量的一般格式为: struct 结构名 { 类型 变量名; 类型 变量名; ... }结构变量; 另一种常 ...
随机推荐
- 在pcDuino上刷了AndDroid,Ubuntu,XBMC
一.Android.Ubuntu.XBMC播放高清视频得比较 1.Andrioid上播放1080P 无压力,硬件解码 2.Ubuntu上用Mplayer播放视频会很卡,可能是没有硬解的原因 3.Ubu ...
- POI使用详解
Apache POI使用详解 1.POI结构与常用类 (1)POI介绍 Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案 ...
- C++重载解析
重载解析(overloading resolution)的规则决定了编译器为一个函数调用选用哪个函数定义.一般过程如下: 将名称相同的函数/模板函数找到并创建候选列表 从中挑选参数数目正确,符合完全匹 ...
- 动画Rig设置为Legacy
The animation state AimUp could not be played because it couldn't be found!Please attach an animatio ...
- [转] linux之sed用法
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为: sed ...
- MyISAM与InnoDB的区别
1. 存储结构: MyISAM:(文件名以表名开始) .frm文件存储表定义 .MYD文件存储数据 .MYI文件存储索引 InnoDB: 所有的表保存在同一个(也可能多个)数据文件中,表的大小仅受限于 ...
- jQuery创建ajax关键词数据搜索
在web开发过程当中,我们经常需要在前台页面输入关键词进行数据的搜索,我们通常使用的搜索方式是将搜索结果用另一个页面显示,这样的方式对于搭建高性能网站来说不是最合适的,今天给大家分享一下如何使用 jQ ...
- JQuery__Tab实践
刚开始学做网站的时候,是从DIV+CSS开始的,那时候不明白“幻灯片”“二级导航”等,更不明白“动态网站”.后来,需要用到幻灯片banner的时候,老师没有仔细讲解JS,就说:从网站找来,会用就好!于 ...
- 关于模板中的动态取值 ---反射与javascript脚本编译
在项目中经常遇到一个问题,打印word或者打印excel的时候,我们经常使用一对一的赋值或者批量替换的方式来对模板进行修改. 但是现在遇到两种场景: 1.取值是通过自定以方法进行取值的. 如:一个销售 ...
- Hive学习之六 《Hive进阶— —hive jdbc》 详解
接Hive学习五 http://www.cnblogs.com/invban/p/5331159.html 一.配置环境变量 hive jdbc的开发,在开发环境中,配置Java环境变量 修改/etc ...