Ren
Ren

Reputation: 4680

C++: Giving value to array of chars

So I have this:

char uloginName[] = "derp";
char mew[33] = "home/home/" << uloginName << "\0";

I am trying concatinate uloginName with the rest of the string that will later be converted to an array of char. But it keeps returning me an error. I don't know how to do it. Also, I must use only char[] type as of this moment; No string.

Thank you for the help.

Upvotes: 0

Views: 185

Answers (3)

johnsyweb
johnsyweb

Reputation: 141958

It looks like you are looking for std::ostringstream, which is a versatile and far less error-prone way of handling strings in C++. strcat(), strncat and their kin are hangovers from C and should be used cautiously in C++.

char uloginName[] = "derp";
std::ostringstream mew;
mew << "home/home/" << uloginName;

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182885

char uloginName[]="derp";
char mew[33]="home/home/";
strcat(mew, uloginName);

You can use strcat on arrays of characters, so long as there is sufficient space and they are terminated with a zero byte.

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249592

Use strncat().

Upvotes: 2

Related Questions