By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,688 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,688 IT Pros & Developers. It's quick & easy.

Is it Nesting structure within itself

P: n/a
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

Aug 26 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
shan wrote:
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."

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 26 '06 #2

P: n/a

Eric Sosman wrote:
shan wrote:
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'.

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

>
--
Eric Sosman
es*****@acm-dot-org.invalid
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

Aug 26 '06 #3

P: n/a
"shan" <sr**********@gmail.comwrites:
typedef struct node {
int data;
struct node *next; //more explanation needed for this line
}sll;

Is *next a variable to struct node,
next is a pointer to struct node.

See http://en.wikipedia.org/wiki/Linked_list
Can anybody explain the need of typedef.I think it's not necessary .
It's not necessary but you don't have to type 'struct node' but simple
'sll'.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
Aug 26 '06 #4

P: n/a

shan wrote:
Eric Sosman wrote:
shan wrote:
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'.

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

--
Eric Sosman
es*****@acm-dot-org.invalid

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.

Aug 26 '06 #5

P: n/a

shan wrote:
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'.
Advance thanks for your answers
Aug 26 '06 #6

P: n/a
Bill Pursell wrote:
shan wrote:
>>Eric Sosman wrote:
>>>shan wrote:

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'.

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

>>>--
Eric Sosman
es*****@acm-dot-org.invalid

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.
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.

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 26 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.