Thread: Why many programmers reluctant to using typedef for struct declarations?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    87

    Why many programmers reluctant to using typedef for struct declarations?

    Are there any potential disadvantages ?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I didn't even know we established that many programmers are actually reluctant to using typedef for struct declarations.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One consequence of typedef is that the fact the type is a struct gets lost. If you have
    Code:
    struct blah 
    {
       ... 
    };
    
    int func(struct blah *ptr)
    {
    ...
    }
    then it's clear that func's argument is a struct.

    If, on the other hand we do:
    Code:
    typedef struct tagblah
    {
    ... 
    } blah;
    
    int func(blah *ptr)
    {
    ...
    }
    It's not so clear from just reading the prototype that it's a structure.

    Another consequence of typedef is that it (used as above) introduces TWO names to the global namespace, rather than one.

    However, I agree with MacGyver, I don't believe that typedef's are underused by "many programmers".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quote Originally Posted by matsp View Post
    Another consequence of typedef is that it (used as above) introduces TWO names to the global namespace, rather than one.
    Code:
    typedef struct
    {
       //... 
    } MyStruct;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    It can't reference itself in that example.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    But you can typedef it to have the same name.

    Code:
    typedef struct blah
    {
        /*...*/
    } blah;
    Blah can reference itself and you can also choose whether you add struct when referring to blah or not.
    Code:
    void foo(struct blah* pb)
    {
        /*...*/
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. Programming Puns
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 03-23-2002, 04:38 PM