C/Cpp的 typedef
参考资料
https://en.wikipedia.org/wiki/Typedef
http://publications.gbdirect.co.uk/c_book/chapter8/typedef.html
Indicating what a variable represents 指示一个新名字来代表
typedef int km_per_hour;
typedef int points;
km_per_hour current_speed; //"km_per_hour" is synonymous with "int" here,
points high_score; //and thus, the compiler treats our new variables as integers.
...
void congratulate(points your_score)
{
if (your_score > high_score)
...
Simplifying a definition or declaration 可以简化声明或定义
struct MyStruct
{
int data1;
char data2;
};
struct MyStruct a;
//使用 typedef后:
typedef struct MyStruct newtype;
newtype a;
------------
//另外两种更简便的方式:
typedef struct MyStruct
{
int data1;
char data2;
} newtype;
typedef struct
{
int data1;
char data2;
} newtype;
Using typedef with pointers 指针
typedef int * intptr; // type name: intptr
// new type: int*
intptr ptr; // same as: int *ptr
例如:
typedef int * intptr;
intptr cliff, allen; // both cliff and allen are int* type
intptr cliff2, *allen2; // cliff2 is int* type, but allen2 is int** type
// same as: intptr cliff2;
// intptr *allen2;
Using typedef with structure pointers 结构体指针
struct Node
{
int data;
struct Node *nextptr;
};
// 想要定义若干结构体类型的指针变量
struct Node *startptr, *endptr, *curptr, *prevptr, errptr, *refptr; //因打字疏忽少输入一个*号,导致errptr并不是指针
// 为避免上面的打字疏忽
typedef struct Node* NodePtr;
NodePtr startptr, endptr, curptr, prevptr, errptr, refptr; //都是结构体指针类型
Using typedef with function pointers 函数指针
函数指针特殊的地方在于其并不是典型的 typedef <old type name> <new alias>;
格式。而是,<return type> <new alias> <argument types>
的格式。
已知普通的函数:
int do_math(float arg1, int arg2)
{
return arg1 + arg2;
}
int call_a_func(int (*call_this)(float, int))
{
int output = call_this(5.5, 7);
return output;
}
int final_result = call_a_func(&do_math);
可以改写成:
typedef int (*MathFunc)(float, int);
int do_math(float arg1, int arg2)
{
return arg2;
}
int call_a_func(MathFunc call_this)
{
int output = call_this(5.5, 7);
return output;
}
int final_result = call_a_func(&do_math);
这里,MathFunc
就是那个 <new alias>
,MathFunc
是一个代表函数的指针,这个指针返回int
、参数是(float, int)
。
例子1:
#include <stdio.h>
// basic function
float do_math(int i, float f)
{
return i + f;
}
float do_math2(int i, float f)
{
return i * f;
}
// param: func pointer
float call_func(float (*func)(int, float))
{
return func(5, 3.5);
}
// using typedef
typedef float (*myfunc_t)(int, float);
float call_func2(myfunc_t func)
{
return func(5, 3.5);
}
int main(int argc, char const *argv[])
{
/*
* 函数的指针:
* `函数名`与`&函数名`同义,都代表函数的指针。
*
* 所以,下面的调用call_func(&do_math)也可以写成call_func(do_math),运行结果相同。
*/
float result1 = call_func(&do_math);
printf("result1 is: %f\n", result1);
float result2 = call_func(&do_math2);
printf("result2 is: %f\n", result2);
float result3 = call_func2(&do_math);
printf("result3 is: %f\n", result3);
float result4 = call_func2(&do_math2);
printf("result4 is: %f\n", result4);
return 0;
}
// 运行结果:
result1 is: 8.500000
result2 is: 17.500000
result3 is: 8.500000
result4 is: 17.500000
例子2:
//原来的函数样子
void (*signal(int sig, void (*func)(int)))(int);
//使用typedef后
typedef void (*sighandler_t)(int);
sighandler_t signal(int sig, sighandler_t func);
Using typedef with arrays 数组
typedef char arrType[6]; // type name: arrType
// new type: char[6]
arrType arr = {1, 2, 3, 4, 5, 6}; // same as: char arr[6]={1,2,3,4,5,6}
arrType *pArr; // same as: char (*pArr)[6];
Using typedef with type casts 类型转换
typedef int (*funcptr)(double); // pointer to function taking a double returning int
funcptr x = (funcptr) NULL; // C or C++
funcptr y = funcptr(NULL); // C++ only
funcptr z = static_cast<funcptr>(NULL); // C++ only
这里,左侧的 funcptr
用于声明变量,右侧的用于类型转换(cast)。
注意,若不使用typedef,一般不太可能像上面那样既声明一个变量同时又用于类型转换。比如:
void *p = NULL;
int (*x)(double) = (int (*)(double)) p; // This is legal
int (*)(double)y = (int (*)(double)) p; // Left-hand side is not legal
int (*z)(double) = (int (*p)(double)); // Right-hand side is not legal