Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to mutually link two structures?

Status
Not open for further replies.

DustyMx

Programmer
Jan 10, 2005
3
0
0
GB
Hi guys, I would like to know if it is possible to declare two structures where struct1 has a pointer to struct2 and struct2 has a pointer to struct1, if it is possible how they should be declared?

typedef struct _AAA {
BBB *p_bbb;
} AAA;

typedef struct _BBB {
AAA *p_aaa;
} BBB;

Thanks
 
One way:
Code:
typedef struct _BBB BBB;
struct _AAA
{
   BBB *p_bbb;
};

typedef struct _AAA AAA;
struct _BBB
{
   AAA *p_aaa;
};
Another way:
Code:
struct _BBB;
struct _AAA
{
   struct _BBB *p_bbb;
};

struct _BBB
{
   struct _AAA *p_aaa;
};
 
could you not use a void pointer as in

Code:
typedef struct _AAA {
void *p_bbb;
} AAA;

typedef struct _BBB {
void *p_aaa;
} BBB;

it woould require a little more work later when
working with the code but you would not have to
wory about having a structure defined in one case
before you could use it in the declaration of the
other.

TC
 
are we assured that globals are created in the
order that we list them. I seem to recall that
we cannot assume what order the compiler will
process globals. and occasionally this will lead to
our assumptions creating a bug. we get away with it
most of the time but then there is that one time.

kinda like returning a pointer to a non static
char array.

TC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top