[英]typedef syntax with struct definition
I have a sample code for a microcontroller. 我有一个微控制器的示例代码。
There is a structure typedef
d as shown below. 有如下所示的typedef
d结构。
typedef struct _AT91S_SYS {
AT91_REG AIC_SMR[32]; // Source Mode Register
AT91_REG AIC_SVR[32]; // Source Vector Register
AT91_REG AIC_IVR; // IRQ Vector Register
...
} AT91S_SYS, *AT91PS_SYS;
I have used typedef
with structs like } AT91S_SYS;
我已经将typedef
与} AT91S_SYS;
类的结构} AT91S_SYS;
. 。
What does this additional part does? 这个额外的部分做什么? *AT91PS_SYS;
in } AT91S_SYS, *AT91PS_SYS;
在} AT91S_SYS, *AT91PS_SYS;
Is it a pointer to the struct _AT91S_SYS
type? 它是struct _AT91S_SYS
类型的指针吗?
AT91_REG
is a typedef
of volatile unsigned int
AT91_REG
是volatile unsigned int
的typedef
This just defines the type AT91PS_SYS
as a pointer to AT91S_SYS
. 这只是将类型AT91PS_SYS
定义为指向AT91S_SYS
的指针。
The easiest way to understand typedef
, by the way, is to read the rest of the declaration as if it were just a variable declaration. 顺便说一句,理解typedef
的最简单方法是读取声明的其余部分,就好像它只是一个变量声明一样。 But, instead of defining variables, you're defining types using whatever type the variable would have had. 但是,不是定义变量,而是使用变量将具有的任何类型来定义类型。
So, for example, 因此,例如
int x, *y, z[5];
defines three variables, int x
, int *y
and int z[5]
. 定义三个变量, int x
, int *y
和int z[5]
。
Therefore, 因此,
typedef int x, *y, z[5];
defines two types, x == int
, y == int *
and z == int[5]
. 定义两种类型, x == int
, y == int *
和z == int[5]
。 Simple! 简单!
Yes, you are right, the syntax is equivalent to this: 是的,您是对的,语法与此等效:
typedef struct _AT91S_SYS AT91S_SYS;
typedef struct _AT91S_SYS *AT91PS_SYS;
So AT91PS_SYS
is a pointer type of AT91S_SYS
. 所以AT91PS_SYS
是一个指针类型AT91S_SYS
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.