#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    1
    Rep Power
    0

    purpose of typedef struct etc. ?


    I see this kind of thing a lot:
    Code:
    typedef struct
    {
    //
    } name;
    
    or this
    
    typedef enum
    {
    //
    } name;
    My question is why do people do this? Does this notation have some kind of special function? Isn't it simpler to write:
    Code:
    struct name
    {
    //
    };
    
    and
    
    enum name
    {
    //
    };
    ????
    Might it have something to do with C vs. C++ if so why do I see this type of notation in purely c++ files which have c++ only code all over the place?
    I have never come across an instance where I would even concieve of using the previous method so if somone would be so kind and explain what the point of that is I would be grateful.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Earth, Milky Way
    Posts
    2
    Rep Power
    0
    Rem: I've never seen that before. :)
    Are you sure the keywords 'struct' and 'enum' did not have anything following them?

    ie, typedef struct blah;
    typedef enum { bleh, blah, blarg } ...

    I would assume you'd get a compilation error with nothing following those keywords like that.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,161
    Rep Power
    2222
    Remington, it's very much a C thing. In C, when you do a
    Code:
    struct name
    {
       /* struct body */
    };
    , then name is a tag. However, in order to use that tag to refer back to that struct, you always need to use the keyword struct; e.g.:
    Code:
    struct name
    {
       /* struct body */
    };
    
    struct name names[];
    
    void ProcessNameStruct(struct name *nom);
    However, if you were to define a struct to be a new data type, then you would not need to use that keyword struct anymore. Then you could write this:
    Code:
    typedef struct 
    {
       /* struct body */
    } name;
    
    name names[];
    
    void ProcessNameStruct(name *nom);
    In C++, you no longer need to use the keywork struct in all non-typedef'd struct references; e.g.:
    Code:
    // the C++ way
    struct name
    {
       // struct body
    };
    
    void ProcessNameStruct(name *nom);
    To emphasize again, this is only an issue in C and not in C++. If you were to compile your second example and use that struct without the struct keyword (as in my last example), then it would succeed in C++ but fail in C.

    As for why you see that strictly C usage in C++ files, the best explanation I can offer is that the programmer had come up through by ranks of C before moving on to C++ or else had been trained by an old C'er. That syntax is still supported by C++, even though it is not required.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    This has more to do with C, rather than C++. If you declare a struct or enum like this:
    Code:
    struct name
    {
    //
    };
    then, when declaring variables for this struct or enum, you have to do this:
    Code:
    struct name foo, bar;
    enum name2 baz, quux;
    If you use a typedef, something like this
    Code:
    typedef struct name {
    //
    } NAME;
    then, when declaring variables for this struct or enum, you can do this:
    Code:
    struct name foo, bar;
    NAME foo, bar;
    The above statements are equivalent. All the typedef statement does is make NAME as synonym for struct name. It is the same thing for enum.

    Now if you do something like this:
    Code:
    typedef struct {
     //
    } NAME;
    Notice that the struct didn't have a name, but the compiler may automatically assign some internal name to it (let's say it is "foobar"). You don't care what this name is, cuz the typedef makes NAME as a synonym for struct (i.e. NAME is a synonym for "struct foobar").

    Notice that in the beginning of the explanation, I said that this had more to do with C than C++. The reason is because, in C++ you can do this:
    Code:
    struct name {
     //
    };
    
    // Declare variables of struct name.
    struct name foo;
    name foo;
    In C++, you can declare foo as a variable of type "struct name" or "name" -- they're both valid ways to do the same thing. In C, only the first declaration is valid.

    Hope this helps :)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    Typedefs in C are not genuinely new types in the same way that say a class is in C++. They are simply aliases for some existing type. Using a typedef in this instance removes the need to explicitly define a tag, and declare each instance with

    struct <tag> <struct_instance> ;

    Instead you have;

    <struct_type> <struct_instance> ;

    This is less cumbersome, especially when passing structure parameters.

    In C++ the structure <tag> also becomes the type name, so there is no need for the typedef.

    Clifford

IMN logo majestic logo threadwatch logo seochat tools logo