anonymous
  • anonymous
Can someone please help me with this function? It's supposed to add the "word" to the array "dictionary" but each time the word just replaces everything else in the dictionary. void newStr(char **&dictionary, int &size, char word[]) { size++; dictionary[size-1]=word; }
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
schrodinger
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
dictionary is defined in the main function as: char **dictionary; dictionary=new char*[size];
rsmith6559
  • rsmith6559
You have more pointers than I'm comfortable with, but wouldn't dictionary be a two dimensional array? The first indice being which word, the second being an array of char that is the word. The pointer to pointer dictionary: Wouldn't the first pointer have to be dereferenced to get the address of dictionary[0][0] ?
anonymous
  • anonymous
it's a really nice question coz i cudnt figure out a solution...i want to get subscribed for this question so i leave this comment

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
Thanks mr smith. dictionary is a double pointer, meaning it points to pointers which point to the first letter of each word. That bit works, I just don't understand why, each time I add a word it replaces all the other words. For example, if I enter "dog" and then "cat", the dictionary will hold the word cat twice with no dog.
rsmith6559
  • rsmith6559
Are you sure that the pointers to the words have different values?
anonymous
  • anonymous
no i tried it too ....all have the same pointers
anonymous
  • anonymous
Ok. If any of you are interested I figures it out. The new pointer has to be dynamically allocated before it can be correctly written in to. Here is the corrected code: void newStr(char **&dictionary, char word[], int &size) { size++; dictionary[size-1]=new char[15]; strcpy(dictionary[size-1], word); } Thanks anyway!
anonymous
  • anonymous
i thot about it too ...but think about it..........dictionary is just an array of pointers....and we did allot memory to those words now we are just storing those values(address) in that array.........so i dont see why it shudnt work i mean consider the construction of a hash table we play the same game there r8? or is it coz we use linked lists there?

Looking for something else?

Not the answer you are looking for? Search for more explanations.