char * is a pointer to some characters somewhere in your memory.
char[] is a block of memory containing characters.
Different concepts really.
If you want to "assign" the pointer to the block, you need to copy the block pointed to by the pointer: this is what strcpy (and strncpy) are for.
// make sure we do not overwrite "msg" (ie prevent writing more bytes than the buffer can accomodate)
// (there are more elaborate ways to do that)
// note esp. that strncpy does [B]not[/B] guarantee the result to be 0-terminated when the input is too large!
strncpy(msg, string.toLatin1().constData(), sizeof(msg));
// therefore, often one does
msg[sizeof(msg)-1]=0;
QString string;
// make sure we do not overwrite "msg" (ie prevent writing more bytes than the buffer can accomodate)
// (there are more elaborate ways to do that)
// note esp. that strncpy does [B]not[/B] guarantee the result to be 0-terminated when the input is too large!
strncpy(msg, string.toLatin1().constData(), sizeof(msg));
// therefore, often one does
msg[sizeof(msg)-1]=0;
To copy to clipboard, switch view to plain text mode
Note that if you need to pass a char* to a C-API you can just pass string.toLatin1().constData() ... the pointer will be til its surrounding expression has been evaluated:
some_c_api(... string.toLatin1().constData(), ...);
some_c_api(... string.toLatin1().constData(), ...);
To copy to clipboard, switch view to plain text mode
However, this would be very bad
const char* msg = string.toLatin1().constData();
some_c_api(... msg, ...); // msg is dangling pointer now!
const char* msg = string.toLatin1().constData();
some_c_api(... msg, ...); // msg is dangling pointer now!
To copy to clipboard, switch view to plain text mode
HTH
Bookmarks