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;
}
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.
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).
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() .
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....
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++.
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
.
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.
err
unnecessaryAs 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) {
...
}
reinterpret_cast<>
encouragedAs 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) {
...
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With