Home:ALL Converter>How to convert const char* to char* in C?

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

Ask Time:2014-08-28T21:06:06         Author:Morpheus

Json Formatter

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*?

Author:Morpheus,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/25549562/how-to-convert-const-char-to-char-in-c
meaning-matters :

To be safe you don't break stuff (for example when these strings are changed in your code or further up), or crash you program (in case the returned string was literal for example like "hello I'm a literal string" and you start to edit it), make a copy of the returned string.\nYou could use strdup() for this, but read the small print. Or you can of course create your own version if it's not there on your platform.",
2014-08-28T13:08:46
j123b567 :

You can use the strdup function which has the following prototype \n\nchar *strdup(const char *s1);\n\n\nExample of use:\n\n#include <string.h>\n\nchar * my_str = strdup(\"My string literal!\");\nchar * my_other_str = strdup(some_const_str);\n\n\nor strcpy/strncpy to your buffer\n\nor rewrite your functions to use const char * as parameter instead of char * where possible so you can preserve the const",
2014-08-28T13:11:57
raghav3276 :

A const to a pointer indicates a \"read-only\" memory location. Whereas the ones without const are a read-write memory areas. So, you \"cannot\" convert a const(read-only location) to a normal(read-write) location. \n\nThe alternate is to copy the data to a different read-write location and pass this pointer to the required function. You may use strdup() to perform this action.",
2014-08-28T13:17:46
Bikash :

To convert a const char* to char* you could create a function like this :\n#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n\nchar* unconstchar(const char* s) {\n if(!s)\n return NULL;\n int i;\n char* res = NULL;\n res = (char*) malloc(strlen(s)+1);\n if(!res){\n fprintf(stderr, "Memory Allocation Failed! Exiting...\\n");\n exit(EXIT_FAILURE);\n } else{\n for (i = 0; s[i] != '\\0'; i++) {\n res[i] = s[i];\n }\n res[i] = '\\0';\n return res;\n }\n}\n\nint main() {\n const char* s = "this is bikash";\n char* p = unconstchar(s);\n printf("%s",p);\n free(p);\n}\n\n",
2019-04-17T02:31:39
dhein :

You can cast it by doing (char *)Identifier_Of_Const_char\n\nBut as there is probabbly a reason that the api doesn't accept const cahr *,\nyou should do this only, if you are sure, the function doesn't try to assign any value in range of your const char* which you casted to a non const one.",
2014-08-28T13:09:41
Wojtek Surowka :

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.\n\nUse cast:\n\n(char*)const_char_ptr\n",
2014-08-28T13:07:14
yy