Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Typedef question (http://www.velocityreviews.com/forums/t551143-typedef-question.html)

Blip 11-11-2007 11:17 PM

Typedef question
 



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


Malcolm McLean 11-11-2007 11:24 PM

Re: Typedef question
 

"Blip" <blip@krumpli.com> wrote in message
news:k83fj391u4746ci5nsprpruinm41p6fa8g@4ax.com...
>
>
>
> 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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


Keith Thompson 11-12-2007 01:55 AM

Re: Typedef question
 
Blip <blip@krumpli.com> writes:
> 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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Barry Schwarz 11-13-2007 03:24 AM

Re: Typedef question
 
On Sun, 11 Nov 2007 18:17:05 -0500, Blip <blip@krumpli.com> wrote:

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


All times are GMT. The time now is 11:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.