68

I used the strcpy() function and it only works if I use C-string arrays like:

char a[6] = "text";
char b[6] = "image";
strcpy(a,b);

but whenever I use

string a = "text";
string b = "image";
strcpy(a,b);

I get this error:

functions.cpp: no matching function for call to strcpy(std::string&, std::string&)

How to copy 2 strings of string data type in C++?

2
  • 17
    Just a=b; :D ____ Commented Oct 1, 2012 at 18:24
  • You should use the std::string type and not use strcpy but copy assignment. Commented Oct 1, 2012 at 18:24

4 Answers 4

97

You shouldn't use strcpy() to copy a std::string, only use it for C-Style strings.

If you want to copy a to b then just use the = operator.

string a = "text";
string b = "image";
b = a;
Sign up to request clarification or add additional context in comments.

6 Comments

and what of strcat () ? is it a=a+"image"; ?
for strcat you can use a+="image"
All of strcpy, strcat, strtok etc. are functions of string.h which operator only on c strings i.e. strings that are mere character arrays terminated by a null. If for convenience you have a string object and you still want to use some string.h function, you can call str.c_str() on the string object which basically returns a c string for the contents of the object. However, this is seldom needed because string object has a pretty good api
@Caesar, please do answer this : link; Thanks.
If for example I would wish for a copy of string that was "on the stack" (ie. say passed by function call) to be put into a struct that lives on the heap, will this still hold?
|
22

strcpy is only for C strings. For std::string you copy it like any C++ object.

std::string a = "text";
std::string b = a; // copy a into b

If you want to concatenate strings you can use the + operator:

std::string a = "text";
std::string b = "image";
a = a + b; // or a += b;

You can even do many at once:

std::string c = a + " " + b + "hello";

Although "hello" + " world" doesn't work as you might expect. You need an explicit std::string to be in there: std::string("Hello") + "world"

7 Comments

and what is the alternative of strcat for std::string ?
@Programer You can combine strings using +: std::string a = "text"; std::string b = "image"; a = a+b;
@PiotrNycz He asks for copying without specifying copy assignment or copy construction, probably because that distinction doesn't make sense for C strings. New C++ programmers typically don't understand the difference between initialization and assignment anyway, and the syntax is deliberately similar. I don't see any reason for the distinction to matter here.
@bames53 OP has two string objects of two distinct values, then wants to overwrite one sting by value of other, This is assignment. But this is your answer not mine. I just thought you overlooked this.
@mLstudent33 No, the std::string copy is entirely independent.
|
4

strcpy example:

#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]="Sample string" ;
  char str2[40] ;
  strcpy (str2,str1) ;
  printf ("str1: %s\n",str1) ;
  return 0 ;
}

Output: str1: Sample string

Your case:

A simple = operator should do the job.

string str1="Sample string" ;
string str2 = str1 ;

1 Comment

does that mean it's not possible to cast from one type string to other type?
4

Caesar's solution is the best in my opinion, but if you still insist to use the strcpy function, then after you have your strings ready:

string a = "text";
string b = "image";

You can try either:

strcpy(a.data(), b.data());

or

strcpy(a.c_str(), b.c_str());

Just call either the data() or c_str() member functions of the std::string class, to get the char* pointer of the string object.

The strcpy() function doesn't have overload to accept two std::string objects as parameters. It has only one overload to accept two char* pointers as parameters.

Both data and c_str return what does strcpy() want exactly.

3 Comments

" It has only one overload". Is this really true? Isn't it the case that it's no overloads? Or that's how I understood the concept of overload.
Should you have the urge to use this, take a cold shower and just don't. The size of the underlying buffer is only guaranteed to be the size of the c-string. Fitting "image" into "text" might work without causing horrible things to happen, but WILL result in the string "imag", since the length of std::string is stored and not evaluated by the terminating character. Plus the second variant will probably not even compile, because c_str() returns a const char *.
Don't ever do this; buffer overflow is very easy to trigger with this. Downvoted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.