Thread: quck question about typedef

  1. #1
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85

    quck question about typedef

    is this:
    Code:
    typedef struct node node_t;
    
    struct node {
    	data_t data;
    	node_t *next;
    };
    the same as this:
    Code:
    typedef struct node {
    	data_t value;
    	node_t *next;
    } node_t;
    ....?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    More or less, yes.

    If you're defining a struct, I believe you have to refer to it by its formal name, however. Meaning that you can't write node_t *next, but would rather have to write it as struct node *next.

  3. #3
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Quote Originally Posted by MacGyver View Post
    More or less, yes.

    If you're defining a struct, I believe you have to refer to it by its formal name, however. Meaning that you can't write node_t *next, but would rather have to write it as struct node *next.
    are you sure?

    edit: oh, you meant inside the actual struct itself

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes, sorry, I meant actually inside the struct itself when you define it. Outside of the actual struct definition, you can do whatever you want with typedefs.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    tnx pplz

  7. #7
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    I've always been curious, is it possible for an anonymous structure to reference itself (i.e. while typedefing it)? I've always found it redundant to give a structure a name that I intend to typedef, and thus never use the structure name ever again. Something along the lines such as this:

    Code:
    typedef struct
    {
        (what to put here?) *ptrAnon;
    } anon;
    At first I thought to just redefine the anonymous struct again while specifying the type of its the pointer ptrAnon, i.e:
    Code:
    typedef struct
    {
        struct{...} *ptrAnon;
    } anon;
    but then that gets funny fast:
    Code:
    typedef struct
    {
        struct{struct{struct{... ad infinitum} *ptrAnon} *ptrAnon} *ptrAnon;
    } anon;
    Now this may very well be the most redundantly stupid question ever, but is it absolutely necessary to give a structure a name if you intend to reference it within itself, or in other words, can an anonymous structure contain a reference to itself?

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Why wouldn't you name your structure though?

    Theres no harm from doing so...

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    typedef struct
    {
    	struct node_t *next;
    } node_t;
    The above works with LCC.

  10. #10
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Quote Originally Posted by zacs7 View Post
    Why wouldn't you name your structure though?
    Theres no harm from doing so...
    Its just all moot really, sort of like anonymous classes in java, you could name them.. but why bother coming up with a unique name if you never intend to use it again.

    Quote Originally Posted by MacGyver View Post
    Code:
    typedef struct
    {
    	struct node_t *next;
    } node_t;
    The above works with LCC.
    Cool, thanks, I'll have to try that out in gcc now.
    ..
    And it works! Awesome!
    Finally no more weird internal names for structures I want to typedef!
    Now I wonder how portable it is... though I'm guessing if its supported in GCC and LCC it's probably standard.

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Depends what flags you used to compile it, why not read the C89 standard regarding structures?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with ZwQueryDirectoryFile for my Driver.
    By taDo in forum Windows Programming
    Replies: 9
    Last Post: 11-27-2008, 08:54 AM
  2. Error: redefinition of typedef xxxx
    By Rocha in forum C Programming
    Replies: 2
    Last Post: 11-24-2008, 09:19 AM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. Help...typedef struct...confusing...
    By darkchild in forum C Programming
    Replies: 1
    Last Post: 01-23-2007, 08:03 AM
  5. question about typedef
    By volk in forum C++ Programming
    Replies: 8
    Last Post: 05-30-2003, 10:53 PM