typedef 及其与struct的结合使用
//相当于为现有类型创建一个别名,或称类型别名。
//整形等
typedef int size; //字符数组
char line[];
char text[];//=> typedef char Line[];
Line text, secondline; //指针
typedef char * pstr;
int mystrcmp(pstr p1, pstr p2);//注:不能写成int mystrcmp(const pstr p1, const pstr p3);因const pstr p1解释为char * const cp(不是简单的替代) //与结构类型组合使用
typedef struct tagMyStruct
{
int iNum;
long lLength;
} MyStruct;//(此处MyStruct为结构类型别名)=> struct tagMyStruct
{
int iNum;
long lLength;
};//+
typedef struct tagMyStruct MyStruct; //结构中包含指向自己的指针用法
typedef struct tagNode
{
char *pItem;
pNode pNext;
} *pNode;//=>error
//1)
typedef struct tagNode
{
char *pItem;
struct tagNode *pNext;
} *pNode;
//2)
typedef struct tagNode *pNode;
struct tagNode
{
char *pItem;
pNode pNext;
};
//3)规范
struct tagNode
{
char *pItem;
struct tagNode *pNext;
};
typedef struct tagNode *pNode; //与define的区别
//1)
typedef char* pStr1;//重新创建名字
#define pStr2 char *//简单文本替换
pStr1 s1, s2;
pStr2 s3, s4;=>pStr2 s3, *s4;
//2)define定义时若定义中有表达式,加括号;typedef则无需。
#define f(x) x*x=>#define f(x) ((x)*(x))
main( )
{
int a=,b=,c;
c=f(a) / f(b);
printf("%d \\n",c);
}
//3)typedef不是简单的文本替换
typedef char * pStr;
char string[] = "abc";
const char *p1 = string;
const pStr p2 = string;=>error
p1++;
p2++; //1) #define宏定义有一个特别的长处:可以使用 #ifdef ,#ifndef等来进行逻辑判断,还可以使用#undef来取消定义。
//2) typedef也有一个特别的长处:它符合范围规则,使用typedef定义的变量类型其作用范围限制在所定义的函数或者文件内(取决于此变量定义的位置),而宏定义则没有这种特性。
//
//C中定义结构类型
typedef struct Student
{
int a;
}Stu;//申明变量Stu stu1;或struct Student stu1;
//或
typedef struct
{
int a;
}Stu;//申明变量Stu stu1; //C++中定义结构类型
struct Student
{
int a;
};//申明变量Student stu2; //C++中使用区别
struct Student
{
int a;
}stu1;//stu1是一个变量 。访问stu1.a typedef struct Student2
{
int a;
}stu2;//stu2是一个结构体类型 访问stu2 s2; s2.a=10;
//还有待增加。
typedef 及其与struct的结合使用的更多相关文章
- 2. struct A 和 typedef struct A
2. struct A 和 typedef struct A 2.1 struct A struct A{}定义一个名为struct A的结构体. 下例定义了struct A同时,声明了两个变量(注意 ...
- Why should we typedef a struct so often in C? - Stack Overflow
https://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c As Greg Hewg ...
- typedef struct 指针结构体使用方法
A>>>>>>>>>>>>>>>>>>>>>>>> ty ...
- typedef定义函数类型或函数指针
转载请标明出处: 最近在看redis的代码,发现了有关函数指针的部分,想把它记下来. 在redis中有类似下面的定义,利用typedef 定义了一个新的类型,这种类型是一个函数: typedef vo ...
- 关于C语言中的typedef
在C语言中定义一个结构体,要最好使用typedef,使用typedef,实际上就是为我们的结构体起了一个新的名字,即定义了一个新的类型,在后面书写自己代码的时候,就可以直接使用自己定义的新的类型第一变 ...
- c语言typedef关键字的理解
1.typedef的定义 很多人认为typedef 是定义新的数据类型,这可能与这个关键字有关.本来嘛,type 是数据类型的意思:def(ine)是定义的意思,合起来就是定义数据类型啦. 不过很遗憾 ...
- 详谈typedef的用法
我们都知道typedef是类型重定义,既然是重定义就不可能出现新的数据类型,只是将已有的数据类型进行换个名字而已,但是这有什么用呢?可能我们学的时候,给的例子都是:typedef int INT; 然 ...
- typedef , static和 extern
typedef 1.作用:给已经存在的类型起一个新的名称 2.使用场合: 1> 基本数据类型 2> 指针 3> 结构体 4> 枚举 5> 指向函数的指针 #include ...
- 转:struct sockaddr与struct sockaddr_in ,struct sockaddr_un的区别和联系
在linux环境下,结构体struct sockaddr在/usr/include/linux/socket.h中定义,具体如下:typedef unsigned short sa_family_t; ...
随机推荐
- systick优先级的理解
sysTick系统嘀嗒定时器并非STM32独有的,它是Cortex内核的部分,CM3为它专门开出一个异常类型,并且在中断向量表中占有一席之地(异常号15).这样它可以很方便的移植到不同厂商出CM3内核 ...
- Opencv 2.4.9在Ubuntu下的配置与安装
[原]Opencv 2.4.9在Ubuntu下的配置安装 Opencv 2.4.9在Ubuntu下的配置与安装 surgewong@gmail.com http://blog.csdn.net/su ...
- C#中调用Windows API的要点 .
介绍 API(Application Programming Interface),我想大家不会陌生,它是我们Windows编程的常客,虽然基于.Net平台的C#有了强大的类库,但是,我们还是不能否认 ...
- Mybatis where 1=1 和 <where>标签
<select id="selSampleListByIDX4" resultMap="BaseResultMap" parameterType=&quo ...
- cmd 登录oracle
源地址:http://zhidao.baidu.com/link?url=mehN7bFY14DGH6DwhpbJnAbzb_fI3WbQn2-WqVInyyqHkfYlZSfu7GQVjQgQoPV ...
- [ActionScript 3.0] AS3.0 对象在一定范围随机显示不重叠
import flash.geom.Rectangle; import flash.display.MovieClip; import flash.display.Sprite; var arr:Ar ...
- [Java] java中方法可以重载
一个类中可以定义不止一个构造器,在使用new创建对象时,Java会根据构造器提供的参数来决定构建哪一个构造器,另外在Java中,Java会同时根据方法名和参数列表来决定所要调用的方法,这叫做方法重载( ...
- 编译 proto 文件到指定语言的代码
由于 Protocol Buffers 3 的正式版还没有发布,在官网(https://developers.google.com/protocol-buffers/docs/downloads)目前 ...
- python学习笔记——异常
转自 http://www.cnblogs.com/rubylouvre/archive/2011/06/22/2086644.html Python内建异常体系结构 BaseException +- ...
- cocso2d-x改变精灵图片
cocos2d-x 改变精灵图片的2种方法. 1. // 首先载入贴图集 CCSpriteBatchNode *spriteBatch=CCSpriteBatchNode::batchNodeWith ...