Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert const char* to char*

Tags:

c++

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) {
        strncpy(error->data,(char*)ptr,LENGTH_ERROR_MESSAGE-1);
        error->data[LENGTH_ERROR_MESSAGE-1] = '\0';
        error->ret = true;
    }
    return size*nmemb;
}
like image 713
Cute Avatar asked May 07 '09 05:05

Cute


People also ask

Can const char * Be Changed?

const char* const says that the pointer can point to a constant char and value of int pointed by this pointer cannot be changed. And we cannot change the value of pointer as well it is now constant and it cannot point to another constant char.

What is the difference between const char * and char * const?

The difference is that const char * is a pointer to a const char , while char * const is a constant pointer to a char . The first, the value being pointed to can't be changed but the pointer can be. The second, the value being pointed at can change but the pointer can't (similar to a reference).

Can you assign a const char * to a string?

You absolutely can assign const char* to std::string , it will get copied though. The other way around requires a call to std::string::c_str() .

What is const char * str?

The const char *Str tells the compiler that the DATA the pointer points too is const . This means, Str can be changed within Func, but *Str cannot. As a copy of the pointer is passed to Func, any changes made to Str are not seen by main....


1 Answers

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.

like image 153
Jared Oberhaus Avatar answered Sep 23 '22 02:09

Jared Oberhaus