typedef struct

This is a discussion on typedef struct within the C Programming forums, part of the General Programming Boards category; ok so I've come to the point where I would like to pass a pointer to a struct to a ...

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    typedef struct

    ok so I've come to the point where I would like to pass a pointer to a struct to a function, however the function's prototype is written before the struct. I realize I could rearrange the code, but by passing a pointer to the struct as a void*, and inside the function casting the pointer to a pointer of the original struct type, I can solve this very easily.

    but my question is, is when I write a typedef struct like so:

    Code:
    typedef struct tagname {
       /* members here */
    } varlist;
    which is the actual new type? is it "tagname" or "varlist"?

    for some reason I think it would make sense if it was the tag name, but I'm unsure of that. if it is the tag name, that means that variable list is still valid (it is not also a list of types), right?

    any help is appreciated. thank you in advance.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    > I realize I could rearrange the code
    You should rearrange the code, so the typedef is in scope for all the things which need to access it.
    Otherwise, it means you can call the function with a pointer to anything, and be completely in the dark as there is no error checking any more.

    > which is the actual new type? is it "tagname" or "varlist"?
    Both these work when declaring variables
    struct tagname foo;
    varlist bar;

    varlist is the name of the new type.
    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
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    Quote Originally Posted by Salem
    > which is the actual new type? is it "tagname" or "varlist"?
    Both these work when declaring variables
    struct tagname foo;
    varlist bar;

    varlist is the name of the new type.
    At the risk of being overly pedantic, that's not precisely true. Typedef does not create a new type; it merely creates a pseudonym (alternate name) for a type. In this instance, the name of the new type is "struct tagname" and varlist is a pseudonym for it.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    typedef struct tagname {
       /* members here */
    } varlist;
    is just a shortcut for
    Code:
    struct tagname {
       /* members here */
    };
    
    typedef struct tagname varlist;
    It's a common enough idiom, although the structure tag (tagname) is often left out. If you can refer to it as varlist, why use struct tagname?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    The historical reason (and the reason that tagname existed in the first place) is that one might want to have one of the members be a pointer to a struct tagname. So this wouldn't work;
    Code:
    typedef struct
    {
        X *x;
    } X;
    and the way to achieve what you want is;
    Code:
    typedef struct tag_x
    {
        struct tag_x *x;
    } X;
    I'm not sure offhand if this requirement was removed from the 1999 C standard or not. But this sort of thing is the reason the feature existed in C originally, still existed in the 1989 C standard, and still exists in the C++ standard (although C++ removed the need for a typedef at all by removing the requirement for the struct keyword to be used, implicitly or explicitly, when creating an instance of a struct).

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, 03:14 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 03:16 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. typedef struct
    By ... in forum C++ Programming
    Replies: 5
    Last Post: 01-19-2004, 03:17 PM
  5. typedef struct question
    By flevine100 in forum C Programming
    Replies: 1
    Last Post: 09-03-2002, 10:34 PM

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