在'C'编程语言中,关键字'typedef'用于为一些对象(struct, array, function..enum类型)声明一个新名称。例如,我将使用struct-s。
在C语言中,我们经常在main函数之外声明一个struct。例如:< / p >
struct complex{ int real_part, img_part }COMPLEX;
main(){
struct KOMPLEKS number; // number type is now a struct type
number.real_part = 3;
number.img_part = -1;
printf("Number: %d.%d i \n",number.real_part, number.img_part);
}
typedef struct complex{int real_part, img_part; }COMPLEX;
//now COMPLEX is the new name for this structure and if I want to use it without
// a keyword like in the first example 'struct complex number'.
main(){
COMPLEX number; // number is now the same type as in the first example
number.real_part = 1;
number.img)part = 5;
printf("%d %d \n", number.real_part, number.img_part);
}
< p > >
typdef通过允许为数据类型创建更有意义的同义词来帮助定义程序的含义和文档。此外,它们还有助于参数化程序以解决可移植性问题(K&R, pg147, C prog lang)
B < p > >
结构体定义了类型。Structs允许方便地对变量集合进行分组,以便将(K&R, pg127, C prog lang.)作为单个单元处理
< p > C >
. type - pedef' struct在上面的a中解释