Thread: why do you typedef a struct?

  1. #1
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96

    why do you typedef a struct?

    Hey guys i was just looking @ some code on the forums and the question popped into my head "Why do people typedef structs?"

    My understanding is that when you make a struct say for example:
    Code:
    struct s
    {
    int x;
    int y;
    };
    's' would already be defined as a type struct and one could type
    Code:
    s new_s1;
    s new_s2;
    so what does setting it's type do thats already not done? to me it seems redundent but i see it all the time so there must be reason for it.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Actually no. If you just define a struct like s you have to use the word struct before it everywhere. So if you have a function for example that takes a pointer to struct s as parameter it's header would look like this:

    void foo(struct s *arg)

    On the other hand, typedefing allows you to use the new type without the keyword struct:

    Code:
    typedef struct s{
      int x;
      int y;
    }my_struct;
    
    /*declare a pointer to this new type*/
    my_struct *t;

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Matty_Alan View Post
    Hey guys i was just looking @ some code on the forums and the question popped into my head "Why do people typedef structs?"
    It is done (ostensibly) to save keystrokes. This time saving is not exactly free though (say for example on a large project, where the struct is defined somewhere in a header file) as future maintenance programmers have to spend more time looking up the definition, just to see what it is they are dealing with.

  4. #4
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by claudiu View Post
    Actually no. If you just define a struct like s you have to use the word struct before it everywhere. So if you have a function for example that takes a pointer to struct s as parameter it's header would look like this:

    void foo(struct s *arg)

    On the other hand, typedefing allows you to use the new type without the keyword struct:

    Code:
    typedef struct s{
      int x;
      int y;
    }my_struct;
    
    /*declare a pointer to this new type*/
    my_struct *t;
    Wouldn't the person actually be *declaring* the struct? Just curious because here is what my computer produces.

    Code:
    struct s
    {
      int x;
      int y;
    };
    
    
    int main(void)
    {
      s.x = 5;
    
      return 0;
    }
    [cd@localhost oakland]$ gcc -Wall -Wextra -ansi stru.c -o stru
    stru.c: In function ‘main’:
    stru.c:10: error: ‘s’ undeclared (first use in this function)
    stru.c:10: error: (Each undeclared identifier is reported only once
    stru.c:10: error: for each function it appears in.)
    [cd@localhost oakland]$
    Also, I thought *defining* a struct introduces struct into main()' scope. Maybe I'll dig out my old co-workers speel on *defining* vs *declaring* structures before laserlight tries to argue with me about it.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Overworked_PhD
    Wouldn't the person actually be *declaring* the struct?
    A definition is also a declaration. This could be a forward declaration of struct s:
    Code:
    struct s;
    This is the definition of struct s:
    Code:
    struct s
    {
        int x;
        int y;
    };
    In your example, this is simply wrong, since s is not an object:
    Code:
    s.x = 5;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Overworked_PhD View Post
    Wouldn't the person actually be *declaring* the struct? Just curious because here is what my computer produces.

    Code:
    struct s
    {
      int x;
      int y;
    };
    
    
    int main(void)
    {
      s.x = 5;
    
      return 0;
    }


    Also, I thought *defining* a struct introduces struct into main()' scope. Maybe I'll dig out my old co-workers speel on *defining* vs *declaring* structures before laserlight tries to argue with me about it.
    Well maybe you should have declared a variable of type s first. S is a type not a variable!

    As for the definition/declaration issue, I was referring to the definition of the type which is I guess a poor choice of words, not the definition in the definition vs declaration sense, although in this case they happen to coincide.

    EDIT: But laserlight pointed out the differences above.
    Last edited by claudiu; 04-18-2010 at 12:35 PM. Reason: addition

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I've always been annoyed that C allows this abuse of its grammar:

    Code:
    typedef struct foo {
       int bar;
       struct foo *next;
    } 
    foo;
    I can't give you language lawyer terms for what's going on here, but typedef works like this.

    Code:
    /* typedef definedType preferredTypeName; */
    
    typedef struct foo foo;
    So it is always valid to define a struct and then rename it later in separate statements. The use of "preferred" in the example is intentional, as you could use either struct foo or foo.

  8. #8
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by laserlight View Post
    A definition is also a declaration. This could be a forward declaration of struct s:
    Code:
    struct s;
    This is the definition of struct s:
    Code:
    struct s
    {
        int x;
        int y;
    };
    In your example, this is simply wrong, since s is not an object:
    Code:
    s.x = 5;
    I knew this would happen... I found old co-workers emails on structure definitions vs structure declarations. They are not the same thing! I guess I tend to believe him over you since

    a)He hold a Master's Degree in Computer Science from MIT.
    b)Currently works as a Software at Google
    c)Has 8 patents to his name
    d)Seems to produce one academic publication every 8 months.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Overworked_PhD
    I knew this would happen... I found old co-workers emails on structure definitions vs structure declarations. They are not the same thing! I guess I tend to believe him over you since
    I did not say that they are the same thing. I said that definitions are declarations. However, declarations are not necessarily definitions. Refer to the C standard:
    Quote Originally Posted by C99 Section 6.7 Paragraph 5
    A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:
    • for an object, causes storage to be reserved for that object;
    • for a function, includes the function body;
    • for an enumeration constant or typedef name, is the (only) declaration of the identifier.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by laserlight View Post
    I did not say that they are the same thing. I said that definitions are declarations. However, declarations are not necessarily definitions. Refer to the C standard:
    I said something that was rather offensive, that's why the nice mod came in and edited this post for me. I promise not to do the same again.

  11. #11
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by Overworked_PhD View Post
    I said something that was rather offensive, that's why the nice mod came in and edited this post for me. I promise not to do the same again.
    Dude, LaserLight may be many things but she is no ones moron. She may be opinionated (aren't we all on here though?) but I would not presume to argue with her on the C or C++ standard. Heck I have been doing this for a couple of decades and I don't keep up on it they way she (and others on here do). well except the parts that interest me but that is a digression. Just sayin.

    As for the typedef business on the one hand it can serve to save keystrokes but on the other hand it can also make code more readable. Consider:
    Code:
    struct TTicket
    {
         int cost;
         char *destination;
    };
    
    int main(int argc, char *argv[])
    {
    
         struct TTicket newTicket;
         ...
    }
    Versus:

    Code:
    typedef struct TTicket
    {
         int cost;
         char *destination;
    }trainTicket;
    
    int main(int argc, char *argv[])
    {
    
         trainTicket newTicket;
         ...
    }
    Note: I did the above pre-coffee so it may not compile but the point should be obvious.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  12. #12
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    At the risk of being edited again, I'm going to say she isn't that bright. I've read some of her past posts. She demonstrates an average grasp at the very best.

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    What's wrong with you man? There's no reason to get personal here IMO.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Are you implying that chapter and verse from the C Standard isn't enough to trump your smart friend?

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    @Overworked_PhD. perhaps you are overworked and should cease to annoy other people on forums to vent off and go for a walk or something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  5. typedef struct question
    By flevine100 in forum C Programming
    Replies: 1
    Last Post: 09-03-2002, 09:34 PM