0

So i have used a trie structure for this pset. And i have declared my struct node in the load section as well as root and current which are nodes. I've written my code for check but I need to access root and current in order to be able to do the checking. Obviously, simply using root and current is not working as they haven't yet been declared (check is before load in dictionary.c), but when used in speller.c, root and current will already exist. Any help on how to access them?

Thanks in advance

*****EDIT*****

So I'm pretty sure I tried what you had suggested before posting and for some reason or another it didn't work. I have just tried again, somehow with fewer errors this time. So I moved the following lines of code from the load function to the top of the dictionary.c file underneath the #include statements.

// set up node struct
typedef struct node
{
    bool is_word;
    struct node* children[27];
}
node;

//initialise root and current
node* root = NULL;
root = malloc(sizeof(node));
node* current = NULL;

Where before (when in load) this was working fine, I now have the following errors...

dictionary.c:27:1: error: type specifier missing, defaults to 'int' [-Werror,-Wimplicit-int] root = malloc(sizeof(node));

^~~~

dictionary.c:27:1: error: redefinition of 'root' with a different type: 'int' vs 'node *' (aka 'struct node ') dictionary.c:26:7: note: previous definition is here node root = NULL;

Any idea what is going on?

Thanks

0

Gee, if only there was a way to delcare them globally, like a global variable.......

oh wait! ;-)

The trick is to know when it really is OK to do something that you ordinarily shouldn't do, or when it's either the best or only way to do something!

3
  • But where should you put the global variable? I would of put it in speller.c but we're not aloud to alter that. Would it be fine in either dictionary.h or dictionary.c? – user9568 Sep 23 '15 at 1:54
  • I put the few global vars that I had, along with any struct defs at the top of dictionary.c, after the includes and before all of the functions. – Cliff B Sep 23 '15 at 2:33
  • Hey guys, thanks for the help, I would appreciate it if you could have a look at my edit and help out with that problem. Thanks! – GCOTT94 Sep 23 '15 at 18:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .