When coding load (using the hashtable method), I declared the structure for a node and an array of nodes within the function. However, this--of course--means I can't use these things in the check function that I'm now trying to write. I'm probably overlooking a simple solution, but where/how should I declare the struct node and node* array so that I can access them in all of the functions in dictionary.c?


A global variable is one that can be used anywhere in the program. Generally, globals are considered to be a bad practice, but there are times where they are perfectly appropriate for the situation. This is one of those cases. (There are those that say a global should NEVER be used. When you find one of those people, always evaluate their advice with an amount of suspicion. However, there should be a really, really good reason for using a global. )

To declare a global variable that can be used anywhere, it needs to be declared outside of, and before, any function or main that uses it. In this case, you could declare in either of two places - at the beginning of the dictionary.c file after the #include statements but before the first function, or in the dictionary.h file.

Note: You can declare a global outside of any function, but most operations to a global must be done inside of a function or main. Doing so outside will usually generate a compile error.


  • Thanks--I added it to beginning of the dictionary.c file and it works! – Andrea Perry Mar 19 '16 at 19:43

Use dictionary.h. Anything global should be declared there.

  • I don't think so. the thing is that these are implementation details that shouldn't be visible to other files in the program (e.g., speller.c which happens to include dictionary.h) simply because we don't need to. this is called information hiding. see Libraries (3:30)! – kzidane Mar 18 '16 at 21:40
  • My understanding of the pset was that you were encouraged to modify dictionary.h with a global declaration of the struct used across all four functions. Maybe I misunderstood that part of it? I submitted everything successfully, so that mode of implementation worked... – Peter Mar 18 '16 at 22:35
  • I never said it wouldn't work that way. it's just that it would be a better design if you hid your implementation details! I'm also not sure when/where exactly we were encouraged to modify dictionary.h. – kzidane Mar 18 '16 at 22:45

declaring your struct as well as your variable globally (i.e., outside of any function in dictioanry.c) should do the job. for more information, see Variables and Scope!

You must log in to answer this question.

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