loop in strucures definition

R

riccardo

Hi,
I've

typedef struct {
foo2 a;
} foo1;

typedef struct {
foo1 b;
} foo2;


foo2 is not defined yet when used to define foo1.
Placing the foo2 definition before foo1 doesn't help of course.
How can I solve this in a clean way?

R
 
N

Noob

riccardo said:
I've

typedef struct {
foo2 a;
} foo1;

typedef struct {
foo1 b;
} foo2;


foo2 is not defined yet when used to define foo1.
Placing the foo2 definition before foo1 doesn't help of course.
How can I solve this in a clean way?

Can you tell what value bar returns?

int bar(void) { return bar( ); }
 
F

Felix Palmen

* Noob said:
Can you tell what value bar returns?

int bar(void) { return bar( ); }

nothing, because it does not terminate. How do you think this relates to
the OP's question?
 
J

John Bode

Hallo riccardo,




Declare the structs first and use these declarations inside.

struct foo1;
struct foo2;

struct foo1
{
  struct foo2 a;

}

Unfortunately, since "struct foo2" is still incomplete at this stage,
you can't declare an instance of it in "struct foo1"; you *can*
declare a pointer to it, though:

struct foo1
{
struct foo2 *a;
};

Circular dependencies are bad juju and should be avoided if possible.
 
F

Felix Palmen

* John Bode said:
Unfortunately, since "struct foo2" is still incomplete at this stage,
you can't declare an instance of it in "struct foo1"; you *can*
declare a pointer to it, though:

struct foo1
{
struct foo2 *a;
};

Circular dependencies are bad juju and should be avoided if possible.

Oh right, I completely missed that one. Indeed, trying to "circular
embed" structures is a paradoxon :)

Regards,
Felix
 
R

riccardo

Unfortunately, since "struct foo2" is still incomplete at this stage,
you can't declare an instance of it in "struct foo1"; you *can*
declare a pointer to it, though:

struct foo1
{
struct foo2 *a;
};
Tricky but effective!
didn't know I did step on such a nasty topic..
hope it was not a FAQ bible question..
RM
 
R

riccardo

You have an object, which we will call a "box". This box contains things
within it, including another box. Since the definition of "box" is
something which contains another box, this second box contains a third
box. The third must contain a fourth, which contains a fifth, which
contains a six, and so on.

Now, if instead of the box including another box, it includes a piece of
paper telling you where another box may be found (or the word "NULL"),
then this infinitely-recursive definition never occurs.

Thanks,
Very good example (I must say I have some reason to have the recursion).
RM
 
L

lawrence.jones

riccardo said:
typedef struct {
foo2 a;
} foo1;

typedef struct {
foo1 b;
} foo2;

foo2 is not defined yet when used to define foo1.
Placing the foo2 definition before foo1 doesn't help of course.
How can I solve this in a clean way?

Method 1:

typedef struct foo1 {
struct foo2 a;
} foo1;

typedef struct foo2 {
struct foo1 b;
} foo2;

Method 2:

typedef struct foo1 foo1;
typedef struct foo2 foo2;
struct foo1 {
foo2 a;
};

struct foo2 {
foo1 b;
};
 
N

Niklas Holsti

China said:
Such a structure would have indefinite size. To make recursive structure, you
need to make at least one reference in the loop into a pointer. (In Algol 68
terminology, the type has to have yin yang.)

Hey, thanks for bringing tears of nostalgia to my eyes! Did you read the
two-level Algol 68 grammar? Did you also, as I did, feel like you were
reading a Prolog interpreter for Algol 68, or at least semantic analyser?
 
B

Ben Bacarisse

Method 1:

typedef struct foo1 {
struct foo2 a;
} foo1;

typedef struct foo2 {
struct foo1 b;
} foo2;

Method 2:

typedef struct foo1 foo1;
typedef struct foo2 foo2;
struct foo1 {
foo2 a;
};

struct foo2 {
foo1 b;
};

I think your were expecting a (and possibly b) to be a pointer. As it
stands, in both your methods struct foo1 has a member with incomplete
type.
 

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,954
Messages
2,570,113
Members
46,700
Latest member
VYJJanette

Latest Threads

Top