简体   繁体   中英

typedef syntax with struct definition

I have a sample code for a microcontroller.

There is a structure typedef d as shown below.

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; .

What does this additional part does? *AT91PS_SYS; in } AT91S_SYS, *AT91PS_SYS;
Is it a pointer to the struct _AT91S_SYS type?

AT91_REG is a typedef of volatile unsigned int

This just defines the type AT91PS_SYS as a pointer to 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. 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] .

Therefore,

typedef int x, *y, z[5];

defines two types, x == int , y == int * and 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM