Thread: declaring a struct with its fields as a struct

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    declaring a struct with its fields as a struct

    hi, i was wondering if someone could tell me what it means to use a 'struct' as its fields?

    typedef struct Node {
    struct Node * pre;
    struct Node * next;
    void * data;
    } Node;

    this would be used for a linked list. why do you add a 'struct' when declaring struct Node * pre;
    thanks. clear help is much appreciated = )

    and also, would omitting Node after the close parenthesis at the end make a difference? i normally see an array but if it isn't, does it make a difference to add it there or not? thanks.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First off your typedef is incorrect... you will probably get a "redefinition of struct Node" error when you try to compile it. Try it like this...
    Code:
    typedef struct t_Node {
        struct t_node * previous;
        struct t_note * Next;
        void * data; 
    } Node.
    Second t_Node* previous; and t_Node* Next; are not structs. They are pointers to structs of type t_node. (That's what the * means). So you are not imbedding structs in structs. You have a struct that contains pointers to other structs in your list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting struct to struct
    By klmdb in forum C Programming
    Replies: 6
    Last Post: 08-14-2010, 02:29 PM
  2. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  3. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  4. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM

Tags for this Thread