typedef syntax with struct definition

176 Views Asked by At

I have a sample code for a microcontroller.

There is a structure typedefd 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

2

There are 2 best solutions below

0
On BEST ANSWER

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.

0
On

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!