我们通常用typedef来重命名某个变量,或将和平台无关的数据类型起个别名。如下
typedef unsigned char UCHAR;
typedef struct boot{
/* xxx */
}S_BOOK;
今天看编码规范发现typedef的几点用法比较奇怪记录下来:
1- typedef int ARR_100[100];
初看感觉很奇怪,使用ARR_100来重命名元素个数为100的数组int [100],感觉写法很奇怪,为什么不是typedef int[100] ARR_100;?
说法1:typedef只能为数据类型定义别名;int是数据类型,int[100]不是数据类型
说法2:语法规定,记住就好。
参考:想问下为什么不写成typedef int[4] int_array;-优快云论坛
帮我解释下这段c++的程序。最好详细点,尤其是typedef int int_array[4];,这个类型后面怎么定义指针了_百度知道
后来强迫自己记录下来,
typedef int ARR_100[100];
ARR_100 tmp;
/*结果为400*/
printf("sizeof ARR_100 : %d\n", sizeof(tmp));
2- tpyedef 重命名结构体
错误定义:
typedef struct book{
S_BOOK tmp_book;
}*S_BOOK;
订正为:
struct book{
struct book *tmp_book;
};
typedef struct book *S_BOOK;
3- typedef和函数指针
typedef int (*f) (int ,char *);
int func(int cnt, char * str)
{
}
f tmp = &func;
(*func)(1, "hello");
4- typedef不是简单的字符串替换
typedef char* PCHAR;
/* 等价于char * const ptmp*/
const PCHAR ptmp;