当前位置:网站首页>【C language】typedef的使用:结构体、基本数据类型、数组

【C language】typedef的使用:结构体、基本数据类型、数组

2022-08-09 11:03:00 XV_

typedef基本数据类型

typedef int a;
a abc;

后面的a abc就等价于int abc

typedef结构体

typedef 
struct a {
    
	int a;
	int b;
} 
abc;

abc aaa;

对于上述,abc aaa;就等价于struct a aaa;

简而言之,typedef的本质,就是构建等价关系

第一个例子,让aint等价;
第二个例子,让abcstruct a { int a; int b; };等价;

这样一来,简化书写。

不过也有特别的例子,就是使用数组的时候。

typedef数组

typedef int a[5];
a aa;

这里a aa等价于int aa[5],这里aa的本质,是具有5个元素的int类型数组

也就是说,typedef int a[5];,使得aint[5]等价,当然C语言没有这样的写法,希望能够理解,a就是代表具有5个int类型元素的数组。

typedef struct desc_struct
{
    		                		
	unsigned long a, b;		
} 
desc_table[256];

desc_table idt, gdt;

这里idt就是struct desc_struct idt[256]gdt同理。

原网站

版权声明
本文为[XV_]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_42929607/article/details/115829523