5

Possible Duplicate:
Convert std::string to const char* or char*

void setVersion(char* buf, std::string version) { buf = version; } 

I'm trying to write the version string into the buf, but the code above gave me this error "cannot convert ‘std::string {aka std::basic_string}’ to ‘char*’ in assignment".

What is the simplest way to fix it?

0

5 Answers 5

13

Assuming buf is at least version.length() + 1 bytes in size:

strcpy(buf, version.c_str()); 
Sign up to request clarification or add additional context in comments.

4 Comments

-1 You can't make such assumptions in correct code.
@JamesKanze, I was implying this as a responsibility of the OP.
@JamesKanze: Yes you can. Since the caller was the one who passed in both the pointer and the string, it is certainly his responsibility to make sure the length of the string does not exceed the size of the buffer.
@BenjaminLindley In which case, there's no point in having the function, since the caller has to do more work than is done in the function.
5

First, there's a serious problem with the interface, since you don't know how large buf is. Without knowing this, there is no way you can correctly write anything to it. If you're passed the length, you can do something like:

void setVersion( char* buffer, size_t size, std::string const& version ) { size_t n = version.copy( buffer, size - 1 ); // leave room for final '\0' buffer[ n ] = '\0'; } 

Another possibility is that the intent is for you to set some global pointer; the given interface can't do this, since you have a copy of the pointer, but if you were given a reference to it, you might do:

void setVersion( char*& buffer, std::string const& version ) { if ( buffer != NULL ) { delete [] buffer; } buffer = new char[ version.size() + 1 ]; size_t n = version.copy( buffer, std::string::npos ); buffer[ n ] = '\0'; } 

(Other versions are possible, but you have to copy the string, to avoid lifetime issues.)

Comments

3

make sure buf has enough space to store the string

strcpy(buf,version.c_str()) 

Comments

2

Try adding a call to c_str() after version:

void setVersion(char* buf, std::string version) { buf = version.c_str(); } 

8 Comments

Your code has no observable behavior.
@BenjaminLindley What do you mean by that?
@BenjaminLindley What you meant?
@Hunter: With strcpy, you are copying to the string that the pointer points at. buf is a local copy of whatever pointer was passed in at the call site. Even though it is itself local, the thing it points to is still the same thing that was pointed to by the pointer that it was copied from. And if your assumption was correct, then there is no need to make the char* a parameter. It should just be declared inside the function.
Also, it would need to be declared as const char *.
|
1

If all you want is a char* of the std::string - as pointed out earlier use vesrion.c_str().

If you do want to copy it into a separate buffer - for the function signature you have used - buffer should be allocated before the function is called and should be of same size as the version.size() + 1.

Otherwise you could do the following:

void setVersion ( char** out, std::string in ) { *out = new char[in.size() + 1]; strcpy ( out, in.c_str() ); } 

HTH

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.