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?

Community
  • 1
  • 1
Terry Li
  • 16,870
  • 30
  • 89
  • 134

5 Answers5

13

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

strcpy(buf, version.c_str());
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • -1 You can't make such assumptions in correct code. – James Kanze Dec 09 '11 at 17:10
  • 4
    @JamesKanze, I was implying this as a responsibility of the OP. – hmjd Dec 09 '11 at 17:12
  • 3
    @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. – Benjamin Lindley Dec 09 '11 at 17:17
  • @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. – James Kanze Dec 09 '11 at 17:27
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.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
3

make sure buf has enough space to store the string

strcpy(buf,version.c_str())
Joseph Stine
  • 1,022
  • 1
  • 12
  • 23
zchenah
  • 2,060
  • 16
  • 30
2

Try adding a call to c_str() after version:

void setVersion(char* buf, std::string version) 
{
  buf = version.c_str();
}
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • 3
    Your code has no observable behavior. – Benjamin Lindley Dec 09 '11 at 16:36
  • @BenjaminLindley What do you mean by that? – Hunter McMillen Dec 09 '11 at 16:36
  • @BenjaminLindley What you meant? – Terry Li Dec 09 '11 at 16:37
  • @Hunter: I mean that when this function is called, it's effects are not visible to the user. buf is a local variable. – Benjamin Lindley Dec 09 '11 at 16:37
  • So is `version`. Even if you make `buf` a `char**`, it would be pointing to invalid memory when the function returns. – matthias Dec 09 '11 at 16:39
  • I was assuming that the OP abstracted away other code in this function, and that `buf` is actually used for something. That may or may not be the case, but even using `strcpy()` wouldn't this still be true? – Hunter McMillen Dec 09 '11 at 16:40
  • 1
    @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. – Benjamin Lindley Dec 09 '11 at 16:43
  • 2
    Also, it would need to be declared as `const char *`. – Benjamin Lindley Dec 09 '11 at 16:53
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

shekhar
  • 1,372
  • 2
  • 16
  • 23