tags:

views:

168

answers:

4

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?

+5  A: 
PMYSTRUCT ms = NULL;

is equal to

MYSTRUCT* ms = NULL;
Johann Gerell
+1  A: 

It will give the same effect as

typedef MYSTRUCT * PMYSTRUCT;

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

Jay
A: 
MY_STRUCT s;
s.a = 10;
PMYSTRUCT ps = &s;
ps->a = 20;
lalitm
@lalithmohan: I believe you meant to use PMYSTRUCT ps = instead of MY_STRUCT ps = Please consider editing your answer.
semaj
A: 

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.

Pavel Radzivilovsky