Typedef question

B

Blip

typedef struct Ctrl_t
{
Int8U *pData;
Int32U Counter;
UsbEpStatus_t EpStatus;
}Ctrl_t, *pCtrl_t;


It appears that two synonyms are defined for the structure but one is
a pointer? What is happening here?

Thanks, Tom
 
M

Malcolm McLean

Blip said:
typedef struct Ctrl_t
{
Int8U *pData;
Int32U Counter;
UsbEpStatus_t EpStatus;
}Ctrl_t, *pCtrl_t;


It appears that two synonyms are defined for the structure but one is
a pointer? What is happening here?
In later code

Ctrl_t block; /* a struct Ctrl-t */
pCtrl_t; blockptr /* a pointer to a struct Ctrl_t */

The notation Ctrl_t *blockptr is far superior, and clearer to anyone who
knows C but not the idiosyncracies of your naming system. However the double
typedef is legitimate.
 
K

Keith Thompson

Blip said:
typedef struct Ctrl_t
{
Int8U *pData;
Int32U Counter;
UsbEpStatus_t EpStatus;
}Ctrl_t, *pCtrl_t;


It appears that two synonyms are defined for the structure but one is
a pointer? What is happening here?

Poor style, IMHO.

There aren't two synonyms for the structure. There's one synonym for
the structure and another synonym for a pointer to the structure.

The stuff starting with "struct Ctrl_t" and ending with "}" declares a
structure type whose name is "struct Ctrl_t".

The "typedef ... Ctrl_t" declares the name "Ctrl_t" as an alias for
the type "struct Ctrl_t". This is a common style; I don't bother with
such typedefs myself, but I won't get into the reasons here, so don't
worry about that.

The "typedef ... *pCtrl_t" declares pCtrl_t as an alias for the type
"struct Ctrl_t*" (i.e., a pointer to your struct). That's considered
by most C programmers to be a bad idea. C uses pointers for many
things (array indexing, emulating pass-by-reference, etc.). Hiding
the pointer-ness of a type behind a typedef name can make the code
that uses it difficult to read and understand.

If you're using an object or value of a pointer type, you *need to
know* that it's a pointer. I suppose the "p" prefix does that, but
the "*" character does it much more clearly.
 
B

Barry Schwarz

typedef struct Ctrl_t
{
Int8U *pData;
Int32U Counter;
UsbEpStatus_t EpStatus;
}Ctrl_t, *pCtrl_t;


It appears that two synonyms are defined for the structure but one is
a pointer? What is happening here?

The type struct Ctrl_t is declared as a complete type. Ctrl_t is
declared as an alias for the type struct Ctrl_t. pCtlr_t is declared
as an alias for the type pointer to struct Ctrl_t.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top