Thread: struct -- typdef/ self referential

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    57

    struct -- typdef/ self referential

    Hello,

    I'm trying to learn about linked lists. What is wrong here?

    Code:
    typedef struct node {
        NODE *pNext;
    } NODE;
    ----------------------------------------
    Here is my complete example program:

    Code:
    typedef struct node {
        NODE *pNext;
    } NODE;
    
    void doSomething(NODE *);
    
    int main(int argc, char *argv[]) {
        NODE *ppp;
    
        ppp = (NODE *) malloc(sizeof(NODE));
    
        doSomething(ppp);
        
    }
    
    void doSomething(NODE *pnode) {
        pnode->pNext = pnode;
    }

    Many thanks for your help!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by cdave
    What is wrong here?

    Code:
    typedef struct node {
        NODE *pNext;
    } NODE;
    The declaration for NODE is not known until after you attempt to use it.
    Code:
    typedef struct node {
        struct node *pNext;
    } NODE;
    Or...
    Code:
    typedef struct node NODE;
    struct node {
        NODE *pNext;
    };
    Last edited by Dave_Sinkula; 04-24-2005 at 08:04 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    Ah! Thanks Dave! It works!

    And there it is on p.128 of "The C Programming Language (2ndEd)":
    "A struct declaration defines a type."

    A type. type type type!

    Now it make sense. Thanks!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    No, I'm still confused.

    Why does this work:
    Code:
    typedef struct node NODE;
    struct node {
        NODE *pNext;
    };
    but this does not work:
    Code:
    typedef NNN XXX;
    typedef int NNN;
    Both have a 1st typedef which depends on the 2nd type definition.


    Your help IS appreciated.
    Last edited by cdave; 04-24-2005 at 08:39 PM.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by cdave
    No, I'm still confused.

    Why does this work:
    Code:
    typedef struct node NODE;
    struct node {
        NODE *pNext;
    };
    but this does not work:
    Code:
    typedef NNN XXX;
    typedef int NNN;
    Both have a 1st typedef which depends on the 2nd type definition.


    Your help IS appreciated.
    The first declares a typedef for an incomplete struct type which is later defined; the second does not.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    Yes, but why?

    Why is it OK in the 1st case to declare a typdef for an incomplete type,
    but in the 2nd case it is illegal to declare a typedef for an incomplete type?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by cdave
    Yes, but why?

    Why is it OK in the 1st case to declare a typdef for an incomplete type,
    but in the 2nd case it is illegal to declare a typedef for an incomplete type?
    Declaring struct node tells the compiler that a certain structure will exist. It doesn't know it's size or other details yet, but pointers to it could be made, for example, because the compiler knows how to handle pointers to structures. It is an incomplete type that may be completed later.

    An NNN is not an incomplete type; it is not a type that the compiler understands.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    Dave, thanks for your help, it is appreciated, especially how quickly
    you responded.

    I'm still confused about these type definitions, but I'm going to move
    on for now.

    You helped me over a hump. Thanks!

    (I now think my confusion is legitimate; it's C and not me)
    Last edited by cdave; 04-25-2005 at 08:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM