How do you convert a variable of type 'const char*' to 'char*'?
Printable View
How do you convert a variable of type 'const char*' to 'char*'?
You don't really - here's why
Accesible memory is either read-only or read/write. You cannot change memory the compiler designated as read-only to be read/write.
const means read-only
try moving the data do a different place in memory that is read/write like:
Code:const char *s="hi there";
char tmp[20];
strcpy(tmp,s);
// tmp is read/write
Generic way:
const char *str = "something";
char * copy = new char[strlen(str)];
strcpy(copy, str);
// don't forget to delete[] copy when you're done
Oh, strdup() does just that, but it uses malloc, so you must free() the copy.