如何将const char *转换为char * [英] How to convert const char* to char*

查看:241
本文介绍了如何将const char *转换为char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以任何实体告诉我如何将const char *转换为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) {
        strncpy(error->data,(char*)ptr,LENGTH_ERROR_MESSAGE-1);
        error->data[LENGTH_ERROR_MESSAGE-1] = '\0';
        error->ret = true;
    }
    return size*nmemb;
}


推荐答案

在该函数的其余部分中使用 err c>,那么为什么要创建它呢?

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"))
{

如果你确实需要它,你真的需要修改它指向什么吗?如果没有,则将其声明为 const

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");

最后,由于cast是这样讨厌的东西,最好使用一个特定的现代风格的cast您要执行的操作。在这种情况下:

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"))
{

这篇关于如何将const char *转换为char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆