Reputation: 27
I am trying to implement a binary tree for our group project. First of here is some code.
Binary tree structure:
typedef struct tBSTNode
{
string *Key;
tVariable_Prom *BSTNodeCont;
struct tBSTNode * LPtr;
struct tBSTNode * RPtr;
} tBSTNodePtr;
The data of the tree (*BSTNodeCont) looks like this:
typedef struct Variable_Prom {
int TYPE;
double DOUBLE;
int INTEGER;
bool BOOL;
string *StringProm;
} tVariable_Prom;
But I also need to store array of pointers to some binary tree Nodes in a few special cases...
So I thinked of adding this line to my data structure:
tBSNodePtr *pointer;
And then malloc it to the custom array size. But here I encounter a problem, because my first structure needs the second structure to be defined at the time, but also my second structure needs the first structure to be defined first (because it is using it).
Is there some header to structures as to functions? Or is there a simple solution to my problem which I am not seeing?
EDIT: info for the questions below. I have included a header file in my .c file wich contains another header file in which is this definition that seems to work.
struct tVariable_Prom;
typedef struct tBSTNode
{
string*Key;
struct tVariable_Prom *BSTNodeCont;
struct tBSTNode * LPtr;
struct tBSTNode * RPtr;
} tBSTNodePtr;
typedef struct Variable_Prom {
int Typ;
double DoUbLe;
int InTeGer;
bool BoOl;
string *StringProm;
struct tBSTNode *pointer;
} tVariable_Prom;
Here is the malloc where foo is of type tVariable_Prom
:
foo.pointer = malloc(2 * sizeof(tBSTNodePtr));
And a function which searches the binary tree by the Node key(Tstr
) and returns address to the Node on success (odkaz3
):
BSTSearch(&odkaz3, *strom, &Tstr);
Then I am trying to assign an adress of another Node, it is done by another function but practically the problem is there:
odkaz3->BSTNodeCont->pointer[0] = *odkaz2;
EDIT2: Ok I started over again reading what you said to me, now I have 2 header files tVariable_Prom.h:
struct tBSTNodePtr;
typedef struct Variable_Prom {
int Typ;
double DoUbLe;
int InTeGer;
bool BoOl;
string *StringProm;
tBSTNodePtr *pointer;
} tVariable_Prom;
tBSTNodePtr.h:
struct tVariable_Prom;
typedef struct tBSTNode {
string *Key;
tVariable_Prom *BSTNodeCont;
struct tBSTNode * LPtr;
struct tBSTNode * RPtr;
} tBSTNodePtr;
I have both header files included in parser.h which is included in parser.c but I do get these errors now:
tBSTNodePtr.h:5:2: error: unknown type name tVariable_Prom
And on line withodkaz4->BSTNodeCont->pointer[1] = *odkaz3;
:
parser.c request for member 'pointer' in something not a structure or union
Upvotes: 1
Views: 97
Reputation: 126203
You seem to be confused about the difference between objects and pointers to objects. You've confusingly typedef
'd tBSTNodePtr
to be the same as tBSTNode
(an object) rather than as a pointer. Calling some non-pointer a pointer is sure to lead to confusion.
You say you want to have an array of pointers in your tVariable_Prom
, but then you define a single pointer and set it to point to an array of objects, not an array of pointers. What you probabaly want is to add
struct tBSTNode **pointers;
to your tVariable_Prom, and allocate it with
foo.pointers = malloc(2 * sizeof(struct tBSTNode *)); // make an array of 2 pointers
Then you can probably do something like
odkaz3->BSTNodeCont->pointers[0] = odkaz2;
where odkaz2
and odkaz3
are point pointers to nodes.
Upvotes: 0
Reputation: 4046
You can use forward declarations.
struct tVariable_Prom;
typedef struct tBSTNode
{
string *Key;
tVariable_Prom *BSTNodeCont;
struct tBSTNode * LPtr;
struct tBSTNode * RPtr;
} tBSTNodePtr;
and vice-versa. And (optionally) put them in two different headers.
Upvotes: 1