How to convert const char* to char*



can any body tell me how to conver const char* to char*?

get_error_from_header(void *ptr, size_t size, size_t nmemb, void *data) {
    ErrorMsg *error = (ErrorMsg *)data;
    char* err = strstr((const char *)ptr,"550");
    //error  cannot convert const char** to char*
    if(err) {
        error->data[LENGTH_ERROR_MESSAGE-1] = '\0';
        error->ret = true;
    return size*nmemb;
9/16/2014 1:28:53 AM

Accepted Answer

You don't appear to use err in the rest of that function, so why bother creating it?

if (NULL != strstr((const char *)ptr, "550"))

If you do need it, will you really need to modify whatever it points to? If not, then declare it as const also:

const char* err = strstr((const char *)ptr, "550");

Finally, as casts are such nasty things, it is best to use a specific modern-style cast for the operation you want to perform. In this case:

if (NULL != strstr(reinterpret_cast<const char *>(ptr), "550"))
5/7/2009 5:48:34 AM

There are a few things I don't understand here. I see that this is tagged for C++/CLI, but what I describe below should be the same as Standard C++.

Doesn't compile

The code you give doesn't compile; get_error_from_header does not specify a return type. In my experiments I made the return type size_t.

Signature of C++ strstr()

The signature for strstr() in the standard C library is:

char *
strstr(const char *s1, const char *s2);

but the signature for strstr() in the C++ library, depending on the overload, is one of:

const char * strstr ( const char * str1, const char * str2 );
      char * strstr (       char * str1, const char * str2 );

I would choose the first overload, because you don't want to modify the string, you only want to read it. Therefore you can change your code to:

const char* err = strstr((const char *)ptr, "550");
if (err != NULL) {

Also, I'm assuming your comment reporting the error:

//error  cannot convert const char** to char*

is a typo: there's no const char** to be seen.

Assignment to err unnecessary

As is pointed out in a previous answer, the use of err to store the result of strstr is unnecessary if all it's used for is checking NULL. Therefore you could use:

if (strstr((const char *)ptr, "550") != NULL) {

Use of reinterpret_cast<> encouraged

As is pointed out in another answer you should be using reinterpret_cast<> instead of C-style casts:

if (strstr(reinterpret_cast<const char *>(ptr), "550") != NULL) {

Use of const_cast<> to strip const

Given the example in the question, I don't see where this is necessary, but if you had a variable that you need to strip of const-ness, you should use the const_cast<> operator. As in:

const char * p1;
char * p2;

p2 = const_cast<char *>(p1);

As is pointed out in a comment, the reason to use const_cast<> operator is so that the author's intention is clear, and also to make it easy to search for the use of const_cast<>; usually stripping const is the source of bugs or a design flaw.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow