1-
tVSCode:ypedef struct ns { int data; struct ns *next;
Do we have to write struct before ns *next ???Code:typedef struct ns { int data; ns *next; /* pointer to next element in list */ } node;
Thanks.
1-
tVSCode:ypedef struct ns { int data; struct ns *next;
Do we have to write struct before ns *next ???Code:typedef struct ns { int data; ns *next; /* pointer to next element in list */ } node;
Thanks.
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.
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
I have no idea what specifically you mean so...
You just created a new data type called node which is the same thing as a struct ns. Got it?Code:typedef struct ns { int data; struct ns *next; /* pointer to next element in list */ } node;
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").
yeah thanks ;-)
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).