I create an instance of a struct, wordTree
, at the base level of my dictionary.c file. When the load
function is called, the intention is that this instance gets created, and "branches" are then added to it, to build it up into a dictionary.
Here's my code:
typedef struct tree {
char *word;
struct tree *left;
struct tree *right;
} tree;
struct tree *wordTree = NULL;
void addWordToTree(char *word, tree *tree) {
//If tree is null, create the tree
if (tree == NULL) {
struct tree *wordTree = &(struct tree) {};
wordTree->word = word;
tree = wordTree;
return;
}
//Otherwise, keep iterating through the given tree,
//eventually inserting the word on a branch
}
bool load(const char *dictionary)
{
//Open dictionary, iterate through it to find the words
//For each word found...
addWordToTree(word, wordTree);
return true;
}
What I'm seeing is that on the first run, wordTree is NULL, as I'd expect. After setting tree
to a non-NULL value in the addWordToTree
function, and then continuing within the load
function, wordTree
appears to continue to be NULL.
I would have expected that referencing a pointer when entering the addWordToTree
function, and then setting it to a new value, would mean that the instance used at the base of the file and within the load
function would point towards the new value.