c - 这是什么类型的 typedef



这是示例代码

typedef int INT, *INTPTR, ONEDARR[10], TWODARR[10][10];

上面的代码是什么意思? INTint的别名。其余代码发生了什么?

问题中的单行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表示类型intINTPTR表示类型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 ) .

最新更新