typedef struct ?

This is a discussion on typedef struct ? within the C Programming forums, part of the General Programming Boards category; When someone declaring typedef struct... Code: typedef struct _MyType //What is this for? { //... } MyType, *PMyType;...

  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
    21,717
    So that they can write MyType instead of struct MyType.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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*.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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?"
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 07:18 AM
  4. typedef struct
    By ... in forum C++ Programming
    Replies: 5
    Last Post: 01-19-2004, 02:17 PM
  5. typedef struct question
    By flevine100 in forum C Programming
    Replies: 1
    Last Post: 09-03-2002, 09:34 PM

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