Tuesday, July 28, 2009

How do I create an array of character pointers?

I'm reading C++ Primer, 4th Edition by S. Lippman. I'm unable to do an exercise in the book:


-- Write a program to read strings into a vector. Now, 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.





Please help. Thanks a bunch!

How do I create an array of character pointers?
char **charPtrArray;





to read a string you can use STL string class


and store this in vector class


define a vector


vector%26lt;string%26gt; myVector;





read some string from console and put it into vector


string myString;


for(int i=0;i%26lt;10;i++)


{


// Read from console and store in myString


cin %26gt;%26gt; myString;





// add your string to vector


myVector.push_back(myString);


}





now allocate memory in charPtrArray for each string and copy your data into it, you can use strcpy for thi.





if further help is needed send a mail to help@paijwar.com

lady palm

No comments:

Post a Comment