简体   繁体   中英

C typedef and pointers to struct

If I have the following:

typedef struct _MY_STRUCT
{
   int a;
   float b;
} MY_STRUCT, *PMYSTRUCT

What does *PMYSTRUCT do? Is it now a pointer type which I need to declare or just a pointer to _MY_STRUCT which I can use?

I know that MY_STRUCT is a new type that needs to be used as follows:

MY_STRUCT str;
str.a = 2;

But what about that *PMYSTRUCT ?

PMYSTRUCT ms = NULL;

等于

MYSTRUCT* ms = NULL;

It will give the same effect as

typedef MYSTRUCT * PMYSTRUCT;

It just acts as a typedef to the pointer of the struct.

MY_STRUCT s;
s.a = 10;
PMYSTRUCT ps = &s;
ps->a = 20;

In c, typedef has a storage class semantics, just like static , auto and extern .

Consider this:

static int a, *p; - declares a to be a static variable of type int , and p to be a static variable of type pointer to int .

typedef int a, *p - declares a to be the type int , and p to be a type pointer to int .

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