I have a very basic question. In C, we declare a structure like so:
Snippet 1:
struct node {
int a;
int b;
char c;
};
I understand the basic concept behind structures. It can be viewed from many angles. It's a "structure", it is used to create a user defined type.
A structure is useless unless we define objects for it. We can create objects for it like so:
struct node obj1;
obj1.a=10; // corresponds to the value of obj1
obj1.c='A'; // ....
and so on..
Okay, Now this following code snippet I cannot understand.
Snippet 2:
struct node {
node* left;
node* right;
int value;
};
- How can we define
node* left
andnode* right
in the structure namednode
when the structurenode
hasn't even been completely formed yet? - What does it even mean? How can it point to something that hasn't even been formed yet?
- How does it even know the size of the object to allocate when we create an object of
node
?
Can anyone help me out on this?
Edit: The code in the question is not valid C code, but it is valid C++ code