How to convert const char* to char* in C?

Morpheus picture Morpheus · Aug 28, 2014 · Viewed 174.3k times · Source

In my project there is a method which only returns a const char*, whereas I need a char* string, as the API doesn't accept const char*.

Any idea how to convert between const char* to char*?

Answer

Wojtek Surowka picture Wojtek Surowka · Aug 28, 2014

First of all you should do such things only if it is really necessary - e.g. to use some old-style API with char* arguments which are not modified. If an API function modifies the string which was const originally, then this is unspecified behaviour, very likely crash.

Use cast:

(char*)const_char_ptr