Thread: Struct containing instances of itself?

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    22

    Struct containing instances of itself?

    Hello, I'm writing a struct "node" like this:
    Code:
    struct node{
      int x;
      int y;
    
    
    };
    Can I make it containing an array of nodes? If I write:
    Code:
    struct node {
      int x;
      int y;
      node nodes[8];
    
    };
    ...it doesnt compile, saying Undefined structure 'node'
    I believe that is because struct node it is not "born" yet when it tries to do that.

    How can I do something similar? Maybe with pointers, but I'm no good with them yet! Thanks a lot

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > node nodes[8];
    Maybe something like
    Code:
    struct simple_node{
      int x;
      int y;
    };
    
    struct node {
      struct simple_node base;
      struct simple_node nodes[8];
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    Yeah, I realized something similar. Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  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