Reputation: 921
I am studying struct
s and somewhere on web I have seen example of "looping struct
." I have three questions (based on the code below):
struct A {
struct B* b; //does not work if it is not a pointer
int data;
};
struct B {
struct A* a; //does not work if it is not a pointer
int data;
};
int main(void)
{
struct A sA;
struct B sB;
sA.b = &sB;
sB.a = &sA;
sA.b->data = 1;
sB.a->data = 2;
printf("a = %d, b = %d", sA.b->data, sB.a->data);
}
1) Why is the struct
member a pointer? Without being the pointer, it does not compile.
2) Did I declare and use the struct
s correctly?
3) I have no idea what a potential use case is for such idiom. Can you give me some examples?
Upvotes: 1
Views: 85
Reputation: 7873
why the inner-struct should be a pointer, with out the pointer it does not compile.
The compiler needs to know the size of your data types, and it figures this out based on the size of their members. It knows what the size of a pointer to a struct
will be, even if it hasn't seen the struct
yet, but it doesn't know the size of the struct
itself.
Did I declared, and used the structs correctly?
Looks fine at first glance. Did you compile it? Did your code run as you expected?
I have no idea what can be a potential use-case for such idiom. Can you give me some examples?
A linked list is the simplest and most common example (a struct
contains a pointer to a struct
of the same type).
Upvotes: 2
Reputation: 17060
What you have here is a two-way relationship. You often see things like this with, for example, a tree; maybe one struct is a parent and the other is a child. The parent has a pointer to each of its children, whereas the child has a pointer to the parent. So why do they need to be pointers? Think about it; if you didn't use a pointer, then struct A would basically need to contain an entire copy of struct B, whereas the purpose of these fields is actually to define the relationship between these structs. You want to say "this struct here is my parent", not "here's a whole other struct that's identical to the one that's my parent."
Upvotes: 1
Reputation: 2901
Think of a struct as a web page and the pointers you declare inside of it as the links on that web page. Replacing a pointer by a value is the same as if you would copy an entire secondary linked web page and paste it where the link in the primary web page was.
If you also have a link in the second web page that pointed back to the first one, you will have a document linking to itself after that copy/pasting.
Now if you think of doing the same (copy/paste) again with that "ego"-pointing link, you would have to paste the web page into a copy of itself and yet you would never eliminate the back-pointing links because one more of this link appears with every copy. And, what's more the document would explode to infinite size.
For the C-Compiler it simply doesn't make any sense to reserve values cyclically into themselves because this would consume all your memory already at the start of your program.
If you leave it with the pointers however, there is no problem at all, like with the two web pages linking back and forth to each other.
Upvotes: 1