I have more than one char arrays to copy into one string or void * or int *. For example,
char c1[] = "Hello";
char c2[] = "World";
char c3[] = "!!!!";
I want to copy into one int* (void*) or string.
I have more than one char arrays to copy into one string or void * or int *. For example,
char c1[] = "Hello";
char c2[] = "World";
char c3[] = "!!!!";
I want to copy into one int* (void*) or string.
In C++ you can simply use + operator
to append strings
string a = "abc";
string b = "dfg";
string c = a + b;
cout << c << endl;
This would be much easier using the std::string
and std::stringstream
namespace class of C++:
#include <sstream>
#include <string>
std::string c1("Hello");
std::string c2("World");
std::string c3("!!!!");
std::stringstream ss;
ss << c1 << c2 << c3;
std::string finalString = ss.str();
You cannot copy these into an int*
or void*
because those are completely different types.
In my opinion the simplest way is the following
#include <iostream>
#include <string>
#include <cstring>
int main()
{
char c1[] = "Hello";
char c2[] = "World";
char c3[] = "!!!!";
size_t n = 0;
for ( char *t : { c1, c2, c3 } ) n += strlen( t );
std::string s;
s.reserve( n );
for ( char *t : { c1, c2, c3 } ) s += t;
std::cout << s << std::endl;
return 0;
}
The output is
HelloWorld!!!!