Structs refering to each other

R

Rick

Hi,

Another question for today, it's about structs refering to each other. I
tried this code

struct List;
struct child
{
struct List parent;
};
struct List
{
struct Child children[];
List parent;
};

As you can see, there's cross-referencing. And that's giving problems. In
the first case, the parent field of the Child struct ain't delcared yet so I
tried to put the Parent above in short, simmiliar as with declaring
functions in top of your C unit. But it didn't work.
And their's another problem, the List field inside the List struct won't
work either, gettings this error "Undefined structure 'List'" (using an old
Borland C++). Can this be fixed?

Greetings,
Rick
 
C

Capstar

Rick said:
Hi,

Another question for today, it's about structs refering to each other. I
tried this code

struct List;
struct child
{
struct List parent;
};
struct List
{
struct Child children[];
List parent;
};

As you can see, there's cross-referencing. And that's giving problems. In
the first case, the parent field of the Child struct ain't delcared yet so I
tried to put the Parent above in short, simmiliar as with declaring
functions in top of your C unit. But it didn't work.
And their's another problem, the List field inside the List struct won't
work either, gettings this error "Undefined structure 'List'" (using an old
Borland C++). Can this be fixed?

I would do it this way:

/*untested*/

typedef struct List list_struct;

struct Child
{
list_struct *parent;
};

struct List
{
struct Child *children[];
list_struct *parent;
};

I use pointers to the parent because I guess not every Child or List has
it's own parent. This way they can point to any parent you'd like.
I also use an array of pointers to children. This might not be
neccesary, and now I think about it, I'd probably use a different
notation: 'struct Child **children;'. To be hounest, I don't know if
this is a big difference, but I works basicly the same. This array of
pointers might be overkill, depending on your needs.

Mark
 
R

Richard Bos

Rick said:
Another question for today, it's about structs refering to each other. I
tried this code

struct List;
struct child
{
struct List parent;
};
struct List
{
struct Child children[];
List parent;
};

As you can see, there's cross-referencing. And that's giving problems.

Yes. Never mind the broken declaration, if you have an entire struct
List within every struct child, and one or more entire struct child
within each struct List, how large do you guess one struct child is
going to be?
What you need is pointers. That way, a struct child does not need to
contain its parent, but merely a pointer to its parent, thus removing
the cyclical definition.
See, for example, <http://www.eskimo.com/~scs/C-faq/q1.14.html>; it
doesn't solve your problem completely, but should point you in the right
direction.

Richard
 
I

Irrwahn Grausewitz

Rick said:
Hi,

Another question for today, it's about structs refering to each other. I
tried this code

struct List;
struct child
{
struct List parent;

parent has an incomplete type; this won't work. You most certainly want
a pointer here:
struct List *parent;
struct List
{
struct Child children[];
struct Child is not defined. You want:
struct child *children;
(Or change child to Child in the first structure declaration.)
List parent;
List is an unknown type. You want:
struct List *parent;

<snip>
HTH
Regards
 
M

Mark Gordon

Hi,

Another question for today, it's about structs refering to each other.
I tried this code

struct List;
struct child
{
struct List parent;

Surely you want to point to the parent, not have the parent entirely
embedded in the child?

struct List *parent;
};
struct List
{
struct Child children[];

You either want
struct Child children[NUM_CHILDREN];
if you have a static number of children (unlikely), or
struct Child *children;
List parent;

List is not a type, it is a structure tag. Also, you can't embed List in
itself for obvious reasons, you want a pointer.

List *parent;
};

As you can see, there's cross-referencing. And that's giving problems.
In the first case, the parent field of the Child struct ain't delcared
yet so I tried to put the Parent above in short, simmiliar as with
declaring functions in top of your C unit. But it didn't work.
And their's another problem, the List field inside the List struct
won't work either, gettings this error "Undefined structure 'List'"
(using an old Borland C++). Can this be fixed?

When using an incomplete type (which is what struct List is until after
it has been fully defined) you can only declare pointers to it, you
can't declare variable or elements of structs of that type for the
obvious reason that the compiler does not know how much space is
required. Also, you could easily end up with a structure of infinite
size, which is what yours would have been had it been allowed.

Please read the FAQ starting with question 1.14
http://www.eskimo.com/~scs/C-faq/q1.14.html

Also please check the FAQ for answers to any other questions before
posting.
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top