Typedef question

Discussion in 'C Programming' started by Blip, Nov 11, 2007.

  1. Blip

    Blip Guest

    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
    Blip, Nov 11, 2007
    #1
    1. Advertising

  2. "Blip" <> wrote in message
    news:...
    >
    >
    >
    > 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
    Malcolm McLean, Nov 11, 2007
    #2
    1. Advertising

  3. Blip <> 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) <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"
    Keith Thompson, Nov 12, 2007
    #3
  4. On Sun, 11 Nov 2007 18:17:05 -0500, Blip <> 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
    Barry Schwarz, Nov 13, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy
    Replies:
    1
    Views:
    335
    Mike Wahler
    Sep 20, 2003
  2. James Brown

    typedef question

    James Brown, Nov 16, 2005, in forum: C Programming
    Replies:
    3
    Views:
    439
    James Brown
    Nov 16, 2005
  3. Tony Johansson
    Replies:
    7
    Views:
    346
    Victor Bazarov
    Aug 16, 2005
  4. Alex

    typedef question

    Alex, Nov 28, 2005, in forum: C++
    Replies:
    6
    Views:
    458
    Alex O.
    Dec 2, 2005
  5. oor
    Replies:
    0
    Views:
    1,343
Loading...

Share This Page