Leonard
Leonard

Reputation: 73

Struct in a struct

Is this usage suitable, and what does it mean?

struct Taken
{
int *p;
struct Taken *previous;
};

Upvotes: 7

Views: 777

Answers (6)

doron
doron

Reputation: 28932

This is the standard element that is used in a linked list.

Upvotes: 0

casablanca
casablanca

Reputation: 70721

struct Taken *previous;

This line declares previous to be a pointer to another structure of the same type Taken -- this can be used to chain together several such structures, for example, to form a linked list. If you're not familiar with this kind of usage, you should probably read up on pointers and their applications.

Upvotes: 3

EboMike
EboMike

Reputation: 77762

It's probably a linked list, but it's not a struct in a struct, it's a pointer to a struct.

Upvotes: 7

Donnie
Donnie

Reputation: 46943

It means that previous is a pointer to a Taken struct. Yes, it is valid.

Upvotes: 0

Timo Geusch
Timo Geusch

Reputation: 24351

Yes, it's a suitable usage and what you're looking at is most likely a node in some sort of linked list.

Upvotes: 2

Jesus Oliva
Jesus Oliva

Reputation: 2302

Yes, that is the typical data structure for a linked list. A linked list is a data structure that consists of a sequence of data records such that in each record there is a field that contains a reference (i.e., a link) to the next/previous record in the sequence

http://en.wikipedia.org/wiki/Linked_list

Upvotes: 2

Related Questions