pleaseINeedHelp,
Here is an example of what you have (more or less) - a string pointer which points to an array of strings (the string pointer is stored in stack memory, and the array of strings is stored in heap memory):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <string>
int main() {
using string = std::string;
using string_pointer = string*;
using integer = int;
const integer number_of_strings = 3;
string_pointer strptr = new string[number_of_strings];
delete[] strptr;
return 0;
}
|
Now, here is an example of an array of string pointers (the array of string pointers is stored in stack memory, and the strings being pointed to are stored in heap memory):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <string>
int main() {
using string = std::string;
using string_pointer = string*;
using integer = int;
const integer number_of_strings = 3;
string_pointer array[number_of_strings] = { nullptr };
for (integer i = 0; i < number_of_strings; ++i) {
array[i] = new string;
}
for (integer i = 0; i < number_of_strings; ++i) {
delete array[i];
array[i] = nullptr;
}
return 0;
}
|
Don't let the heap and stack memory stuff confuse you too much, I'm just being very explicit.
Unless your instructor taught you the difference, it's very likely that they just weren't being precise in their assignment instructions, and that they do actually want you to allocate an array using
new[]
(as you have done).
However, I'm certain at this point that your instructor wants you to use C-strings. The instructions say that you must allocate memory based on how long the string is, which is something you do not have to do with std::string's, as they are designed to shrink and grow by themselves. This only really applies to C-strings, since they are basically just character arrays, which is why I think you must use them (and, by extension, you mustn't mix C-strings and std::string's).