Reputation: 33
C syntax question.
I am looking to make a declaration something like the following:
struct thingy {
struct thingy* pointer;
}
static struct thingy thing1 = {
.pointer = &thing2
};
static struct thingy thing2 = {
.pointer = &thing1
};
I have tried declaring and initializing separately, as in:
struct thingy {
struct thingy* pointer;
}
static struct thingy thing1;
static struct thingy thing2;
thing1 = {
.pointer = &thing2
};
thing2 = {
.pointer = &thing1
};
However I am not sure whether I can declare and initialize static variables separately
Is there a way I can actually get these to point to each other from compilation?
Upvotes: 3
Views: 1041
Reputation: 70502
You were almost there. You need to "forward declare" (actually, this is a tentative definition, thanks AndreyT!) the static instances first, and then initialize their definitions with the desired pointers.
static struct thingy thing1;
static struct thingy thing2;
static struct thingy thing1 = {
.pointer = &thing2
};
static struct thingy thing2 = {
.pointer = &thing1
};
Technically, you only need to forward declare tentatively define thing2
.
Upvotes: 3