Thread: typedef struct

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    465

    typedef struct

    ive seen this a lot, and i have a friend that recently adopted the habit without knowing why its done.

    what i am referring to is this:

    Code:
    typedef struct _tagNameOfStruct
    {
       // stuff
    } NameOfStruct;
    making a typedef of the struct rather than just letting the struct be its own definition.

    can someone explain to me the purpose/benefit of doing it this way?
    I came up with a cool phrase to put down here, but i forgot it...

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    That is a C technique, but in C++ there is no need to do this that I know of. Just use the struct keyword like you use the class keyword (except for the different default access specification):
    Code:
    struct NameOfStruct
    {
      // stuff ...
    };

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    even still, what is the reason for doing it?
    I came up with a cool phrase to put down here, but i forgot it...

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i have a friend that recently adopted the habit without knowing why its done
    That's pretty stupid. Why adopt a habit when you don't know its advantages and disadvantages?

    >can someone explain to me the purpose/benefit of doing it this way?
    In C++ there is no purpose or benefit, but in C it allows the programmer to create variables of the struct type without using the struct keyword:
    Code:
    struct test0 {
      ...
    };
    
    typedef struct test1 {
      ..
    } test1;
    
    struct test0 t0; // Legal C
    test0 t1; // Illegal C
    
    struct test1 t2; // Legal C
    test1 t3; // Legal C
    Notice also that both the structure tag and the typedef name can be the same.
    My best code is written with the delete key.

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    In C if you don't typedef you have to do the following to declare a structure variable.
    Code:
    struct NameOfStruct var;
          instead of
    NameOfStruct var;
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    ah... i see...

    thanks for the info
    I came up with a cool phrase to put down here, but i forgot it...

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, 02:14 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  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