Thread: Can you use a structure in the declaration of itself?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    Unhappy Can you use a structure in the declaration of itself?

    The code below comes from the begginers guide under linked lists. I am confused by it. This is what it says to me:

    Create a structure called 'node' with two variables, int x and node *next.

    How can you declare the node inside itself?

    Im sorry if this is easy for you, I havent coded for a while, like 8 years and Pascal was as close as I got to C++. But time has come for me to go at it again. Love the stuff. Besides, there are no stupid questions, LOL.

    Thanks all for you time and support.

    Code:
    struct node {
      int x;
      node *next;
    };

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Create a structure called 'node' with two variables, int x and node *next.
    Correct.

    >How can you declare the node inside itself?
    You're confusing this:
    Code:
    struct foo {
      foo *bar;
    };
    With this:
    Code:
    struct foo {
      foo bar;
    };
    The latter is illegal because you can't define an object of an incomplete type, and foo is incomplete at the time that you try to define bar. However, the former is just peachy because you can have a pointer to an incomplete type. It works for the same reason that this works:
    Code:
    struct foo;
    
    foo *bar;
    
    struct foo {
      int x;
    };
    Confused yet?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    Yes Im confused, thanks very much. I laughed out loud when I read "just peachy".

    So declaring the pointer as a pointer node, really only delcares a pointer var, giving allocation of name and memory for an address?

    It doesnt really set up a structure of type node with pointers ... ok wait, I have to think about this with paper....before I ask whatever it is Im about to ask.....

    In college my teachers said I could get the hard things easy and the easy things I struggled through....LOL.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So declaring the pointer as a pointer node, really only delcares a pointer var,
    >giving allocation of name and memory for an address?
    Yep. A pointer is a separate type from the thingie it points to. So int and int* are two completely different types. It confuses a lot of people because the only physical difference is that little asterisk, but the semantic differences are pretty extensive.

    >I have to think about this with paper....before I ask whatever it is Im about to ask.....
    Think of "node *next" as a link rather than a node. A node is what a link points to. Also, a link refers only to a potential node, which means if you follow the link, you might or might not find a node. That's where the sentinel (usually just a null pointer) comes in, so that you know you're at the end of the list.
    My best code is written with the delete key.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You pretty much got it, right there. You can't define an object of an incomplete type because the size of the object isn't known at the time of definition, however, a pointer is a pointer... it's the same size no matter the type, so the type really means nothing until it's dereferenced.

    EDIT: ...and I lose. *crawls away*
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    Ok, I tried to diagram on paper, easier for my dislexic brain to understand. It wasnt a pretty picture.

    I quess I can understand a node with a pointer as its last var pointing to wherever.

    But this pointer is a node of a node etc... so does it create a node type with two pointers in it? one to the int x and one for the new node created?

    Maybe I should finishing reading the examples and I will get it.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >however, a pointer is a pointer... it's the same size no matter the type
    How about this one?
    Code:
    void *p;
    Or this one?
    Code:
    int (*fooby)();
    Beware using absolute statements in my presence.

    >But this pointer is a node of a node etc...
    Nope, it's just a pointer. One node has one pointer. If that pointer points to another node, that node also has a pointer. You're trying to apply a recursive definition on a type that simply doesn't have one, despite how it seems at first glance.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    Ok, so reading from above, sayine node *next, doesnt create a node/structure of pointers, just a pointer. So the syntax is for clarity only, and the pointer is part of the structure.

    So why not just say int *ptr?

    Thanks very much and so FAST?

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    Yep, making something simple hard.

    Thank you very much. Have a great day.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    Reading the thread above it makes me look like a dope, but I figured it out the same time as you posted. Oh well, I suppose that happens a lot.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So why not just say int *ptr?
    Because a pointer to foo isn't the same as a pointer to int. The two have different types, and the type of anything is important information. If the type of the pointer and the type of the pointed to object don't match, the chances of your program crashing are pretty good.
    My best code is written with the delete key.

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Prelude
    How about this one?
    Code:
    void *p;
    Or this one?
    Code:
    int (*fooby)();
    Beware using absolute statements in my presence.
    Let me explain that I haven't programmed in so long that I couldn't even tell you off the top of my head what the sizes of those pointers are or why... I'm in over my head on Physics and Calculus. I'll assume you are right... like I always do.
    Sent from my iPadŽ

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    >If the type of the pointer and the type of the pointed to object don't match, the chances of your program crashing are pretty good.

    ahha. So the information, oh you know I need to read more.... I always thought the pointer was just a number, the address of the item being pointed to. So the 'typed' pointer has some data with regard to the item it points too? not just the address?

    And you would declare char *ptr, or int*ptr, or floating *ptr etc ?

    Thanks, I will read on and laugh later at my public questions. I havent coded in anything except html for a long time, big difference.

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    So, typed pointers allow the compiler to generate the proper code for the length of the object being pointed to. In this instance a structure of int length.

    I need to go back to school, LOL, get my masters..... thanks again wont bother you for a while.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I always thought the pointer was just a number, the address of the item being pointed to.
    In the end, yes. But since this isn't assembly, we're not quite at the end. Which is a good thing, because I rather enjoy not having to calculate my own pointer offsets.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM