0

I have been working on the dictionary's "load" function in Problem Set 5, but I have a problem. I don't want to hard-code names for each node within my trie, which I am working on, but in order to initialize the top level, I need to initialize 27 at a time. My current code is somewhat like:

for(int i = 0; i < 27; i++)
{
    struct node i; //this is the problem; they all end up named i
}

Is there a way to initialize variables like this inside the loop, but with different names?

1
  • Maybe i am wrong but i don't think its possible, besides do you realize that when the program will exit the loop all that you declare inside will be out of scope. – wallek876 Aug 13 '15 at 20:25
2

Why not just create an array of pointers to nodes?

struct node* foo[27];

Then you can use a loop to assign NULL to each element by indexing through the array.

If this answers your question, please click the check to accept, and remove your question from the unanswered question pool. Let's keep up on forum maintenance. ;-)

3
  • If I do that, how do I put them into the top level, which is a char array? Or should I not use a char array? – Aidan N Aug 13 '15 at 20:34
  • Well, I'm not sure what you're trying to do, but most people create a struct that contains the array, plus a bool to mark the end of the word. (I assume you're making a trie.) You should go back and review the class material and shorts on creating structures. – Cliff B Aug 13 '15 at 20:39
  • Makes sense now; I was stuck on the idea of initializing a char array to hold the nodes, then putting the nodes on. This makes more sense now that I think about it; thanks. – Aidan N Aug 13 '15 at 20:53

You must log in to answer this question.

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