这是示例代码
typedef int INT, *INTPTR, ONEDARR[10], TWODARR[10][10];
上面的代码是什么意思? INT
是int
的别名。其余代码发生了什么?
问题中的单行typedef
是
typedef int INT;
typedef int *INTPTR;
typedef int ONEDARR[10];
typedef int TWODARR[10][10];
则 INT
是类型 int
的别名。
INTPTR
是类型 int *
的别名。
ONEDARR
是类型 int [10]
的别名。
TWODARR
是类型 int [10][10]
的别名。
(https://en.cppreference.com/w/c/language/typedef(
考虑以下声明
int INT, *INTPTR, ONEDARR[10], TWODARR[10][10];
它用类型说明符int
声明了四个变量:
scalar variable INT
pointer *INTPTR
one-dimensional array ONEDARR[10]
and two-dimensional array TWODARR[10][10]
然后使用 typedef
typedef int INT, *INTPTR, ONEDARR[10], TWODARR[10][10];
然后,变量的名称不表示对象,而是具有变量的类型的别名(如果它们是在没有 typedef 的情况下声明的(。
所以INT
表示类型int
,INTPTR
表示类型int *
,ONEDARR
表示类型int[10]
,TWODARR
表示类型int[10][10]
。
所以现在你可以选择是否按以下方式声明数组
int a'10][10];
或者使用数组类型的别名指定其类型
TWODARR a;
再举一个例子。
假设你有一个函数声明
int f( int x, int y );
它具有类型 int( int, int )
.现在,您要将此类型命名为int( int, int )
使用较短的记录,而不是此长记录。然后你可以使用像 typedef
typedef int FUNC( int x, int y );
因此,名称FUNC
现在表示类型 int( int, int )
.