Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String connection in C++

Status
Not open for further replies.

LaoMa

Programmer
Aug 24, 2002
10
US
Hi,I wonder how to connect 2 strings in c++ under Unix?
I tried strcat,and the code got stuck(no compiling error,though).
This is what I'm doing:
char* s1="bin";
char* s2="***";
...
cout<<strcat(s1,s2)<<endl;

simply doesn't work! The execution stops at the strcat.
 
If you're using C++, you should probably become familiar with the string class. With that, your code becomes:

[tt]string s1 = &quot;bin&quot;;
string s2 = &quot;***&quot;;

cout << s1 + s2 << endl;
[/tt]

As far as your code not working, s1 needs to be a non-const char *; Even though you've declared it as a regular char *, it should be a const char * because it's pointing to a sting literal. Your code will compile (because no one ever used to use const with strings, so they let you do it), but something weird will probably happen at the strcpy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top