How to instantiate multiple objects C
Hi,
I have the following problem:
I have a program Tree.c that reads a text file written in a particular format. From that information, it creates a tree.
i.e. for the following text file:
Code:
16 17 18 0
17 0 0 0
18 19 20 0
19 0 0 0
20 0 0 0
It will create the following tree:
16
_____|_____
17 18
_____|_____
19 20
I have a function that returns me the pointer to the root of the tree: i.e. node 16
I can invoke this function with different text files having the configurations written in them. And, I will create various trees, and get a pointer to the roots.
Let us say,
Code:
// we have three files:
file1, file2, file3
// and, we create three trees,
Tree root1 = create_tree(file1);
Tree root2 = create_tree(file2);
Tree root3 = create_tree(file3);
The problem is the following:
Although the tree is create, I lose control of the root node. In the caller program, only root3 is asserted to be the root. If I remove the third call, then root2 will be asserted as the root of the tree,
i.e. the information for previous roots is overwritten.
Is there a way in C, to make this act as a class, from which we can create objects, and each one retains its properties.
Thanks,