Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

dr25161 Group Title

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

  • one year ago
  • one year ago

  • This Question is Closed
  1. dr25161 Group Title
    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];

    • one year ago
  2. rsmith6559 Group Title
    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] ?

    • one year ago
  3. A.Avinash_Goutham Group Title
    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

    • one year ago
  4. dr25161 Group Title
    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.

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

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

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

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

    • one year ago
  7. dr25161 Group Title
    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!

    • one year ago
  8. A.Avinash_Goutham Group Title
    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?

    • one year ago
    • Attachments:

See more questions >>>

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.