I want to convert a char*
line of commands into a string array. What I tried:
void parse(char* cmd, int length) {
std::string *arguments = nullptr;
int amount = 0;
for (int i = 0; i < length; i++) {
if (cmd[i] == ' ' || cmd[i] == '\t' || cmd[i] == '\n') {
amount++;
continue;
}
arguments[amount] = cmd[i];
}
}
But I'm not sure about the std::string *arguments = nullptr;
part. I don't know the amount of arguments in the cmd
so I'm not sure how to initialize properly the arguments
array. In C I probably should solve it with malloc but how do I solve it in C++? Also, as I want to return arguments
and amount
, how can I pass them as arguments references to the parse
string?