structs A <-> B: forward declaration?

Posted By: Superku

structs A <-> B: forward declaration? - 11/26/16 19:50

Hello!
I have two structs, like this:

Code:
[???]
struct A
{
	int x;
	struct B *b;
};

struct B
{
	int y;
	[...]
	struct A *a;
};


struct A now needs a forward declaration of struct B (right?). No matter what I write, the lite-C compiler only gives me error messages (for example a simple "struct B;" results in "B (Struct) undeclared identifier" in the same line).
The only workaround I can think of right now is using a void pointer in A. Any ideas/ what am I doing wrong?


EDIT:
Does it do any harm if I declare/ define struct B first with let's say one dummy member, then define it differently some lines later? Does not work. Second definition is ignored.
Posted By: Kartoffel

Re: structs A <-> B: forward declaration? - 11/27/16 11:58

You could try a forward declaration, not sure if lite-c supports it, though.
Otherwise, using a void* might be the only solution.

Edit: looks like lite-c doesn't support it..
Posted By: Dico

Re: structs A <-> B: forward declaration? - 11/27/16 13:00

I think it's the limit of litec .
Posted By: Ch40zzC0d3r

Re: structs A <-> B: forward declaration? - 11/27/16 13:23

void* and cast will work fine even though it looks bad.
Maybe you should switch to C++ instead? grin
Posted By: Superku

Re: structs A <-> B: forward declaration? - 11/27/16 18:26

Yeah well, so it's not possible in lite-C, right? -.-
I have it all changed to void pointers now. It works with casting but as the (new) project is heavily based on a lot of those structs it gets quite messy, esp. when there are more than 1 void-cast-dereference-operations (like ((B*)((A*)a)->b)->...).
Posted By: Kartoffel

Re: structs A <-> B: forward declaration? - 11/28/16 21:21

Kinda sucks...

I'm not sure how much code you've already written with your structs (might be a lot of work to change everything) but maybe there's a way to avoid this kind of "loop" definition. I guess it depends on the specific situation but I once ran into the same problem and was able to work my way around it with some changes to the structs I use and how they work together.
Posted By: Superku

Re: structs A <-> B: forward declaration? - 11/29/16 13:45

That's true, maybe I can find a better system/ approach. However, it's the easiest this way (I think) because I can for example just loop through all OBJECTs in a ROOM or know in which ROOM an OBJECT is (without having to use search functions or additional structures).
Posted By: Kartoffel

Re: structs A <-> B: forward declaration? - 11/29/16 18:27

Yeah, I don't see a fancy way of avoiding that problem in this case... so void* is pretty much all that's left.

I think I'd use a void pointer (like void * pParentRoom;) in the OBJECT struct since it's probably not being used as much as the pointer for the other direction. And everytime I want to access it I'd just quickly cast it:

ROOM * pRoom = object->pParentRoom;
pRoom->...


But that's probably how you're doing it already so... grin
Posted By: Superku

Re: structs A <-> B: forward declaration? - 11/30/16 12:26

Yeah I'm doing it like that. wink It's not a game breaker but it's annoying extra work/ lines of code, esp. because there's a bunch of nested structs, now with a void pointer each, already (like door->room->area->superarea and so on).
© 2024 lite-C Forums