Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

dr25161

  • one year ago

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; }

  • This Question is Closed
  1. dr25161
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    dictionary is defined in the main function as: char **dictionary; dictionary=new char*[size];

  2. rsmith6559
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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] ?

  3. A.Avinash_Goutham
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  4. dr25161
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  5. rsmith6559
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Are you sure that the pointers to the words have different values?

  6. A.Avinash_Goutham
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    no i tried it too ....all have the same pointers

  7. dr25161
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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!

  8. A.Avinash_Goutham
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

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

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.