Thread: Question regarding typedef

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    Question regarding typedef

    Hello!

    I have a question regarding typedef use.Why can't we declare a structure like this:



    Code:
    typedef struct
    {  int value;
            element* next;
    } element;
    but instead it must be like this:


    Code:
    typedef struct
    {  int value;
            struct element* next;
    } element;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am not so strong at C syntax, but I believe it should be:
    Code:
    typedef struct element
    {
        int value;
        struct element* next;
    } element;
    The typedef of struct element to element does not exist within the struct definition, thus you have to use struct element instead of just element.

    In your examples, the an anonymous struct is typedef to element, so I reason that it cannot refer to itself since it is anonymous.
    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
    Join Date
    Feb 2006
    Posts
    54
    You could also do
    Code:
    typedef struct element element;
    struct element {
      int value;
      element* next;
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with ZwQueryDirectoryFile for my Driver.
    By taDo in forum Windows Programming
    Replies: 9
    Last Post: 11-27-2008, 08:54 AM
  2. Error: redefinition of typedef xxxx
    By Rocha in forum C Programming
    Replies: 2
    Last Post: 11-24-2008, 09:19 AM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. Help...typedef struct...confusing...
    By darkchild in forum C Programming
    Replies: 1
    Last Post: 01-23-2007, 08:03 AM
  5. question about typedef
    By volk in forum C++ Programming
    Replies: 8
    Last Post: 05-30-2003, 10:53 PM