Create Node

This is a discussion on Create Node within the C Programming forums, part of the General Programming Boards category; 1- t Code: ypedef struct ns { int data; struct ns *next; VS Code: typedef struct ns { int data; ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Create Node

    1-

    t
    Code:
    ypedef struct ns
    {
            int data;
            struct ns *next;
    VS

    Code:
    typedef struct ns
    {
            int data;
           ns *next; /* pointer to next element in list */
    } node;
    Do we have to write struct before ns *next ???


    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yes.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Can you ever leave out the struct? Had your example been written differently I could see room for confusion. But it isn't so I don't.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    So if I have this right:

    "typedef struct ns" creates a new data type.
    And every time I want to make a variable of this type i have to precede it by "struct ns".

    But rather than doing that I can also precede it by node.

    But why create 2 ways of doing the same thing?


    Thanks

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I have no idea what specifically you mean so...

    Code:
    typedef struct ns
    {
      int data;
      struct ns *next; /* pointer to next element in list */
    } node;
    You just created a new data type called node which is the same thing as a struct ns. Got it?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    But more importantly, you didn't do so until you got to the node; at the end of the line -- until that part, i.e. inside the curly braces, it's still called "struct ns". Afterwards, it's called both "struct ns" and "node" (but never just "ns").

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Talking

    yeah thanks ;-)

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by tabstop View Post
    But more importantly, you didn't do so until you got to the node; at the end of the line -- until that part, i.e. inside the curly braces, it's still called "struct ns". Afterwards, it's called both "struct ns" and "node" (but never just "ns").
    Which is what I meant when I said you didn't write your code in a way that generates typical confusion. Your compiler needs to know what an object is in its entirety before allowing you to use it. Though you can always prototype structs... But for the sake of simplicity the correct way to do it is the second way you demonstrated with the correction you had asked about (as shown in my previous post).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Replies: 3
    Last Post: 12-06-2008, 06:54 PM
  3. question about a working linked list
    By cold_dog in forum C++ Programming
    Replies: 23
    Last Post: 09-13-2006, 01:00 AM
  4. Translating Java to C. Mergesorting linked list.
    By Mikro in forum C Programming
    Replies: 10
    Last Post: 06-22-2004, 11:34 AM
  5. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21