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; ...
随机推荐
- Ubuntu编写开机自启动脚本(转载)
From:http://blog.csdn.net/marujunyy/article/details/8466255 1.首先编写一个简单的shell脚本test.sh #! /bin/bash e ...
- Async/Await - Best Practices in Asynchronous Programming z
These days there’s a wealth of information about the new async and await support in the Microsoft .N ...
- Android开发者需要面对的8大挑战
移动开发变得越来越受欢迎,但移动开发者正面临着一系列挑战.本文将介绍的是Android开发者需要面对的8个不利因素,例如缺乏硬件标准化,以及软件碎片.为Android OS开发app,给予了开发人员极 ...
- C++学习18 派生类的析构函数
和构造函数类似,析构函数也是不能被继承的. 创建派生类对象时,构造函数的调用顺序和继承顺序相同,先执行基类构造函数,然后再执行派生类的构造函数.但是对于析构函数,调用顺序恰好相反,即先执行派生类的析构 ...
- [Java] java中的接口定义
在Java的通常规范中,对数据成员的修改要通过接口提供的方法进行(如下面示例中接口中的void learnMath(int hours)和void learnEnglish(int hours)),这 ...
- 从linux内核代码分析操作系统启动过程
朱宇轲 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 在本次的实验中, ...
- Kinect测量人体身高的程序
对着书上敲得,从中体会kinect骨骼识别与深度识别的原理.大体原理是懂了,但有些细节还没有完全弄明白. using System; using System.Collections.Generic; ...
- Troubleshooting 'library cache: mutex X' Waits.
What is a 'library cache: mutex X' wait? The mutex feature is a mechanism to control access to in me ...
- Redis应用
一.什么是Redis? Redis是一个高性能的key-value内存数据库. 二.为什么使用Redis? Redis是NoSQL数据库,相比传统关系型数据库,内存数据库读写更快. 三.Redis怎么 ...
- Android——inflate 将一个xml中定义的布局找出来
通俗的说,inflate就相当于将一个xml中定义的布局找出来. 因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组 ...