Let assume that the actions are done consequently, i.e. first you already done this:
std::string result;
for (int i=0; i < 2; i++)
for (int j=0; j < len; j++)
result.push_back(Array[i][j]);
then you have already a char* array stored inside std::string
.
char *string = new char[result.size()];
string = strdup(test1.c_str());
Now if you skip the std::string step (why, platform doesn't support it?), you rather get something like that:
char *string = new char[string_size]; // you have to either measure it beforehand or calculate from array size
int counter = 0;
for (int i=0; i < 2; i++)
for (int j=0; j < len; j++)
string[counter++] = Array[i][j];
If that's C code, replace new
expression with malloc
call. And this code got flaws like you either should allocate more memory than needed or you should count length of buffer before allocating it. Extreme case is to reallocate buffer when its capacity reached. std::string
does that automatically.
I don't see why this can be a problem to devise, unless you're failing to tell us something important about what you need.
In general C++ code never looks like that unless you're designing your own containers. On ISO-compatible platform you'll have std::string, on most embedded systems, e.g. Arduino, you'll have access to String class.