Thread: typedef struct ?

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    typedef struct ?

    When someone declaring typedef struct...
    Code:
    typedef struct _MyType //What is this for?
    {
       //...
    
    } MyType, *PMyType;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So that they can write MyType instead of struct MyType.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Actually the can write _Mytype instead of struct _Mytype.
    MyType and *PMyType are variable declarations

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually the can write _Mytpe instead of struct _Mtype.
    No, this is C, not C++.

    MyType and *PMyType are variable declarations
    No, because of the typedef keyword. MyType is a typedef for struct _Mytype, PMyType is a typedef for struct _MyType*.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Personnaly, from what I understand of your question, someone would write something like

    Code:
    typedef struct _MyType //What is this for?
    {
       //...
    
    } MyType, *PMyType;
    because it's the only way for a struct to contain a pointer on itself; example
    Code:
    typedef struct _MyType //What is this for?
    {
       struct _MyType *next;
       MyType *next; // Wouldn't work since MyType is not known at this point
    
    } MyType, *PMyType;
    I hate real numbers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... foxman appears to have interpreted the question more accurately than I have, and if that was indeed your intention, audinue, then I would say that foxman's answer is correct.

    Incidentally, note that C99 section 7.1.3 states that "all identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use", so by right one should not use _MyType as a type name unless one is writing the implementation of the core language or the standard library.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Thanks foxman!

    hmm... foxman appears to have interpreted the question more accurately than I have
    Is my English bad T_T??
    Last edited by audinue; 06-22-2008 at 10:45 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is my English bad T_T??
    No, but you could have elaborated by asking "why do we need to provide a type name at this point when we are already providing a type name with the typedef?"
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    No, but you could have elaborated by asking "why do we need to provide a type name at this point when we are already providing a type name with the typedef?"
    That's a long one But, sorry for my undescriptive question.

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. 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, 09:34 PM