Thread: Excercise help again

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What you are trying to do is to create one dynamically allocated array of characters, and then copy the strings to it. This is not what you have been asked to do. Read your instructions again: "copy that vector into an array of character pointers. For each element in the vector, allocate a new character array and copy the data from the vector element into that character array. Then insert a pointer to the character array into the array of character pointers."

    So, you should create a dynamically allocated array of character pointers. Then you should create a dynamically allocated array of characters for each string in the vector, and copy the respective string to the respective dynamically allocated array of characters, and then copy the pointer to each of these arrays over to the array of character pointers.

    By the way, now is a good time to begin dividing your program into parts. Instead of lumping everything into the main() function, create a function to read into the vector. Then create a function that answers the core of the question. Then create another function to print the strings out for checking.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    Sorry for my ignorance but when you say an array of character pointers it means it will store the pointers to the dynamically allocated character array which stores the string element?

    When I insert a pointer into the array of pointers does it copy the pointer over creating a new one, or does it make a pointer to that pointer? My heads sore...

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry for my ignorance but when you say an array of character pointers it means it will store the pointers to the dynamically allocated character array which stores the string element?
    Yes.

    When I insert a pointer into the array of pointers does it copy the pointer over creating a new one, or does it make a pointer to that pointer?
    It will just copy over the pointer. In fact, you do not even need to create a new pointer since you can use the current pointer from the array of pointers.

    My heads sore...
    I think that's what they are trying to make you feel so that you will appreciate the power of using containers and string classes
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just learning, problem with an excercise.
    By medeshago in forum C Programming
    Replies: 2
    Last Post: 10-16-2008, 09:23 PM
  2. Just learning, problem with an excercise.
    By medeshago in forum C Programming
    Replies: 8
    Last Post: 10-15-2008, 07:10 PM
  3. excercise
    By luigi40 in forum C# Programming
    Replies: 9
    Last Post: 11-22-2005, 03:25 AM
  4. Creative Excercise
    By ... in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 01-29-2003, 10:18 AM