//至关于为现有类型建立一个别名,或称类型别名。
//整形等
typedef int size;数组
//字符数组
char line[81];
char text[81];//=>函数
typedef char Line[81];
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(不是简单的替代)string
//与结构类型组合使用
typedef struct tagMyStruct
{
int iNum;
long lLength;
} MyStruct;//(此处MyStruct为结构类型别名)=>变量
struct tagMyStruct
{
int iNum;
long lLength;
};//+
typedef struct tagMyStruct MyStruct;error
//结构中包含指向本身的指针用法
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=6,b=2,c;
c=f(a) / f(b);
printf("%d \\n",c);
}
//3)typedef不是简单的文本替换
typedef char * pStr;
char string[4] = "abc";
const char *p1 = string;
const pStr p2 = string;=>error
p1++;
p2++;co
//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;//还有待增长。