Is it Nesting structure within itself

S

shan

I used the following code in linked list in C.

typedef struct node
{
int data;
struct node *next; //more explanation needed for this line
}sll;


Is *next a variable to struct node,If yes then why it is not given
outside.

Can anybody explain the need of typedef.I think it's not necessary .

Advance thanks for your answers
 
E

Eric Sosman

shan said:
I used the following code in linked list in C.

typedef struct node
{
int data;
struct node *next; //more explanation needed for this line
}sll;


Is *next a variable to struct node,If yes then why it is not given
outside.

Each `struct node' instance contains two elements. The
element named `data' is an int, and the one named `next' is
a pointer. "A pointer" is not a complete description, because
it omits the type of the thing the pointer points to: You can
have pointers to char, pointers to double, and so on. In this
case, the `next' element points to a `struct node'.

Some people find this confusing, because it looks like a
`struct node' contains a `struct node', which itself would
contain a `struct node', and so on in infinite regression.
But that is not the case: `next' is not a `struct node', but
a pointer to a `struct node', which is quite a different thing.
Your house cannot contain another house which contains yet
another house, but your house *can* contain a slip of paper
with another house's address written on it.
Can anybody explain the need of typedef.I think it's not necessary .

It is not necessary. The typedef introduces `sll' as an
alias for `struct node', so you can write `struct node' or `sll'
interchangeably. Some people like the convenience of using a
meaningful alias, other people think it is a silly attempt to
hide the "struct-ness."
 
S

shan

Eric said:
Each `struct node' instance contains two elements. The
element named `data' is an int, and the one named `next' is
a pointer. "A pointer" is not a complete description, because
it omits the type of the thing the pointer points to: You can
have pointers to char, pointers to double, and so on. In this
case, the `next' element points to a `struct node'.

`next' is not a `struct node', but
a pointer to a `struct node', which is quite a different thing.

MR. Eric Sosman

your explanation seems to be like this,Is it correct ?


typedef struct node
{
int data;
}sll,*next;

Can you differentiate between this code and the previous one.

Thanks for your answers & too for future reply
 
B

Bill Pursell

shan said:
MR. Eric Sosman

your explanation seems to be like this,Is it correct ?


typedef struct node
{
int data;
}sll,*next;

Can you differentiate between this code and the previous one.

This is radically different, and is infact a syntax error. Assuming
you meant:

struct node {
int data;
} sll, *next;

this would declare two variables: one named sll, and one named next.
sll is a struct node. next is a pointer to a struct node. However,
there is no way to really use this in a list. Each struct node needs
to contain a pointer to the next entry in the list. If the list has
more than one element, then there must be more than one next
pointer.
 
R

ragi

shan said:
I used the following code in linked list in C.

typedef struct node
{
int data;
struct node *next; //more explanation needed for this line
}sll;


Is *next a variable to struct node,If yes then why it is not given
outside.


Here, next is pointer variable that ill contain address of a stracture
variable of type 'struct node'. It is not at all a nesting of
stracture.
Can anybody explain the need of typedef.I think it's not necessary .

Typedef is optional. But using typedef we can create user defined
datatype.

If you are not using typedef there, you have to use it like 'struct
node vari'. when you use typedef you can simply declare this stracture
variable like 'sll vari'.
 
E

Eric Sosman

Bill said:
This is radically different, and is infact a syntax error.

It's radically different, but I see no syntax error -- no
error at all, in fact. It declares three things:

- A struct type named `struct node' containing one `int'
element named `data'.

- An alias for the type `struct node' called `sll'.

- An alias for the type `struct node *' called `next'.

Once the declaration has been processed, I can write things
like

struct node x; /* x is a struct node instance */
sll y[10]; /* y is an array of struct node instances */
struct node *p1; /* p1 can point to a struct node */
sll *p2; /* p2 can point to a struct node */
next p3; /* p3 can point to a struct node */

Personally, I dislike making typedef aliases for pointer
types most of the time -- but it's legal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top