What is the difference between char * and const_cast <char *> (string.c_str ())

I am using an external library to communicate with udp (OSC) between two applications. To format the messages to be sent, the library expects char *, but I get the string from the user interface that I need to convert.

While I was dealing with other parts of my code, the udp part was hardcoded as follows:

char* endofMess = "from setEndMess"; 

and worked fine. I thought it would be easy to get him to work with his strings and write:

  std::string s = "from setEndMess"; char* endofMess = const_cast<char*>(s.c_str()); 

but unlike the first example, when I received a message correctly formatted, now I get only gibberish characters. Does anyone know where this might come from?

Thanks!

Mathieu

EDIT: The code I'm using: A way to send a message every time OSCVal changes:

 void osc500::testOSC(int identifier, float OSCval) { UdpTransmitSocket transmitSocket( IpEndpointName( destIP, port ) ); char buffer[1024]; osc::OutboundPacketStream p( buffer, 1024 ); p << osc::BeginBundleImmediate << osc::BeginMessage( endofMess ) << OSCval << osc::EndMessage << osc::EndBundle; transmitSocket.Send( p.Data(), p.Size() ); } 

And if I need to change the OSC pattern, I call this:

 void osc500::setEndMess(String endpattern){ // endofMess = "from setEndMess"; //OK works fine each time it called //1st try : //std::string s = "from setEndMess"; //endofMess = const_cast<char*>(s.c_str()); //gibberish //2nd try : //std::string s = "from setEndMess"; //endofMess = &s[0]; //gibberish //3rd & 4th tries : //char s[4] = {'t','e','s','t'}; //char s[5] = {'t','e','s','t','\0'}; //endofMess = s; //gibberish } 
+4
source share
3 answers

I suspect that char * is not written, it just is not const, because it is an outdated API. If so, your problem probably is that std :: string went out of scope or was changed between the point where you call c_str and where it is used in the guts of the API.

+3
source

c_str() is for read-only access std::string .

If you want to write a string using pointers, use either ...

  • array (or vector) char instead of std::string
  • char* buf = &str[0]; - point directly to the first character of the string

Trick (2) is guaranteed to work under C ++ 11; in practice, it also works in C ++ 03, but it relies on the implementation of the std::string compiler with the contignuos repository.

(And whatever you do, keep an eye on the length of the buffer.)

+3
source

If you want to change the contents of std::string , I think you should use &s[0] (make sure the string is big enough).

 std::string s = "abcdef..."; char* ptr = &s[0]; 

eg. (verified using MSVC10):

 #include <iostream> #include <ostream> #include <string> using namespace std; void f(char* ptr, size_t count) { for (size_t i = 0; i < count; i++) ptr[i] = 'x'; } int main() { string s = "abcdef"; cout << s << endl; f(&s[0], 3); cout << s << endl; } 

Output:

 abcdef xxxdef 
0
source

Source: https://habr.com/ru/post/1446742/


All Articles