Excercise help again

This is a discussion on Excercise help again within the C++ Programming forums, part of the General Programming Boards category; What you are trying to do is to create one dynamically allocated array of characters, and then copy the strings ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    gin
    gin is offline
    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
    20,968
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
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, 02:25 AM
  4. Creative Excercise
    By ... in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 01-29-2003, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21