Hello, I've tryed to assign fields in a string to the element of a vector of pointers to char. Because the number of fields in the string is unknown I've declared a vector of pointers, Fields=new char *[], and for each field in the string a pointer to char, Field[i]=new char [Field_Len]. When program show the content of each element, all element have the right string except the first one which contains a wrong string. Some can help me to understand way?
The problem disappears if I allocate memory for a defined number of element, Fields=new char *[20]: in this case the first element is shown correctly; in this case, if the number of fields in the string exceed 20 can I add more elements to Fields using the new operator?
so you have declared an array of char pointers but you haven't allocated memory for the array itself (the new char *[] doesn't do the trick), when you later write Fields[i] = new char [Field_Len] you are doing something naughty
I would propose instead that you use std::vector instead, it easier to use and safer:
Hi anders43, I'm going to study the class <vector> as you suggested , but what is naughty and why in that I did. Can you tell me if the length of object defined as vector may be extended as needed? once program has allocated memory for a given number of element, is it possible to allocate memory for other additional elements?