A question about typedefs

This is a discussion on A question about typedefs within the C Programming forums, part of the General Programming Boards category; Hi! I'm just learning about C/C++, and I would like to ask the experts here about the difference between the ...

  1. #1
    cbc
    cbc is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    4

    A question about typedefs

    Hi!

    I'm just learning about C/C++, and I would like to ask the experts here about the difference between the two pieces of code below. From what I know (which is not a lot), these two pieces of code should be the same. But somehow, I feel that there's a subtle difference, since the author from whom I got this code knows his stuff and deliberately chose to write these two pieces of code this way.

    Code:
    // first typedef //////////////////////////////////
    
    typedef struct
    {
        //fields here...
    } firstStruct;
    
    
    // second typedef /////////////////////////////
    
    typedef struct struct2 secondStruct;
    struct struct2
    {
        //fields here...
    };
    So my question is: is there a difference in these two typedefs? Because if there are none, he should have just written the second struct in the same way as the first one, saving himself additional typing.

    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Since you asked on the C board, I'll give you the C answer (despite you writing C/C++). As you may guess from this, the explanation for C++ is slightly different.

    In the first case, the struct does not have a name (it is anonymous), and it can only be referred to via its typedef name (firstStruct).

    The second struct has a name (struct2), and also a typedef name (secondStruct).

    Giving a structure a name is necessary when you're creating self-referencing structures like linked lists.

    In the first case, you can do
    firstStruct a_variable;

    In the second case, you can do
    secondStruct a_variable;
    struct struct2 another_variable;
    Both amount to meaning the same thing
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Both amount to meaning the same thing
    Unless we're talking about self-referential structures. Then the first typedef would fail.
    My best code is written with the delete key.

  4. #4
    cbc
    cbc is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    4
    Why, thank you very much for your answers. I realized that the second struct was indeed a self-referring structure used in a linked list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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