Killzone Kid
Killzone Kid

Reputation: 6240

Appending to char array with sprintf

So it is tempting to do this:

char buf[1024] = "this is";
std::sprintf(buf, "%s test", buf);

But it is undefined behaviour. I suppose it can be solved via temporary:

char buf[1024] = "this is";
std::sprintf(buf, "%s test", std::string(buf).c_str());

What are the downsides of this approach?

Upvotes: 2

Views: 1431

Answers (1)

KamilCuk
KamilCuk

Reputation: 141155

To append a string to a char array use strcat:

char buf[1024] = "this is";
std::strcat(buf, " test");

Upvotes: 6

Related Questions