typedef struct vs. struct

This is a discussion on typedef struct vs. struct within the A Brief History of Cprogramming.com forums, part of the Community Boards category; why do this: typedef struct tagMyStruct { int x, y; } myStruct; When it is much easier to do this: ...

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738

    typedef struct vs. struct

    why do this:

    typedef struct tagMyStruct
    {
    int x, y;
    } myStruct;

    When it is much easier to do this:

    struct myStruct
    {
    int x, y;
    };

    I see no advantages to the typedef version. So why is it used by lots of people?
    My Website

    "Circular logic is good because it is."

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    because some people code in C not C++ and they want to be able to refer to their structs without typing 'struct' each time.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    typedef struct == bad
    struct == good

    The only thing typedef does in this situation is hide the fact that the programmer is using a struct. If you don't know what datatype the variable is in huge code that just makes reading and maintaining the code more difficult.

    Translation: It's bad style for lazy people and should be avoided in anything but tiny programs.

    >I see no advantages to the typedef version. So why is it used by lots of people?
    That's because there are none.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    *shrug*
    Code:
    int main (void)
    {
     struct node * list;
     //
    }
    vs...
    int main (void)
    {
     node * list;
     //
    }
    It is used purely for the sake of convenience. Putting 'struct' in front of every declaration of some kind of variable doesn't really tell you much about it. In fact, I'd say that having to state 'struct' each time would more likely confuse things, since it's the only case where a storage specifier is more than one word long.

    If I could rewrite C, changing the way structs and unions (and maybe enums) are declared would be on my potential to-do list.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    26
    And if you use all uppercase letters for your struct names like the FILE structure for example, there is no confusion.
    Translation: It's bad style for lazy people and should be avoided in anything but tiny programs.
    I don't see how it's lazy, if you wrote struct in front of your declarations you still would have to see the definitions to see what variables it contained.
    Last edited by Edge; 01-26-2002 at 04:50 AM.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I usually use uppercase letters for names for constants. Names of structures I usually postfix (if that's correct English) by _s.

    typedef struct node node_s;

    And also for enums the postfix _e and there are some more. It's not a matter of lazyness, but a matter of making code more readable.

    Documentation of code usually includes a document explaining the code conventions used and a document describing the detailed design. So when a new engineer gets into the project he/she doesn't need to figure out in the code what certain types mean.

    In huge code especially a definition of clear code conventions is very important. If the conventions are good and the engineers do use the conventions in their code, then working with huge amounts of code can be quite easy.

    So I don't mind people using typedefs as long as they're clear and well described.

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    >The only thing typedef does in this situation is hide the fact that >the programmer is using a struct.

    It creates an Aliasname for your new datatype. It saves you a totally unneccessary information of 'struct' before each variable declaration. Everyone knows it's some kind of user defined variable.

    >If you don't know what datatype the variable is in huge code >that just makes reading and maintaining the code more difficult.

    Shouldn't that be clear by the type name ?

    >Translation:
    >It's bad style for lazy people and should be avoided
    >in anything but tiny programs.

    I don't think so. It saves typing and reading later.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  8. #8
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    i with nv
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  5. typedef struct
    By ... in forum C++ Programming
    Replies: 5
    Last Post: 01-19-2004, 02:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21