简体   繁体   English

如何将 const char* 转换为 char*

[英]How to convert const char* to char*

can any body tell me how to conver const char* to char*?任何人都可以告诉我如何将 const char* 转换为 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;
}

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++ .我看到这被标记为C++/CLI ,但我在下面描述的应该与标准 C++相同。

Doesn't compile不编译

The code you give doesn't compile;您提供的代码无法编译; get_error_from_header does not specify a return type. get_error_from_header不指定返回类型。 In my experiments I made the return type size_t .在我的实验中,我创建了返回类型size_t

Signature of C++ strstr() C++ strstr()签名

The signature for strstr() in the standard C library is:标准 C 库中strstr()的签名是:

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

but the signature for strstr() in the C++ library , depending on the overload, is one of:但是C++ 库中strstr()的签名,取决于重载,是以下之一:

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.是一个错字:没有可以看到的const char**

Assignment to err unnecessary不必要的err赋值

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 .正如在之前的答案中指出的那样,如果err仅用于检查NULL则不需要使用err来存储strstr的结果。 Therefore you could use:因此,您可以使用:

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

Use of reinterpret_cast<> encouraged鼓励使用reinterpret_cast<>

As is pointed out in another answer you should be using reinterpret_cast<> instead of C-style casts:正如在另一个答案中指出的那样,您应该使用reinterpret_cast<>而不是 C 风格的强制转换:

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

Use of const_cast<> to strip const使用const_cast<> 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.鉴于问题中的示例,我看不出这有什么必要,但是如果您有一个需要去除const -ness 的变量,则应该使用const_cast<>运算符。 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<> ;正如评论中指出的那样,使用const_cast<>运算符的原因是为了使作者的意图明确,并且也便于搜索使用const_cast<> usually stripping const is the source of bugs or a design flaw.通常剥离 const 是错误或设计缺陷的来源。

You don't appear to use err in the rest of that function, so why bother creating it?您似乎没有在该函数的其余部分使用err ,那为什么还要创建它呢?

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

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

Can't you just do:你不能只做:

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

The error is because if you pass in a const char* to strstr you get one out (because of the overload).错误是因为如果您将 const char* 传递给 strstr ,则会得到一个(因为过载)。

//try this instead: //试试这个:

const char* mstr="";
char* str=const_cast<char*>(mstr);

According to my deep research I have found numerous forums that have no direct solution or a reference answer to this question, I then delve into the GCC online documentation giving a brief read for their compiler properly docs and this is what I can provide.根据我的深入研究,我发现许多论坛都没有直接解决方案或对此问题的参考答案,然后我深入研究了 GCC 在线文档,简要阅读了他们的编译器正确文档,这就是我可以提供的。

In GNU C, pointers to arrays with qualifiers work similar to pointers to other qualified types.在 GNU C 中,指向带有限定符的数组的指针的工作方式类似于指向其他限定类型的指针。 For example, a value of type int ( )[5] can be used to initialize a variable of type const int ( )[5].例如,int ( )[5]类型的值可用于初始化 const int ( )[5]类型的变量 These types however aren't compatible in ISO C because the const qualifier is formally attached to the element type of the array and not the array itself.然而,这些类型在 ISO C 中不兼容,因为 const 限定符正式附加到数组的元素类型而不是数组本身。

This description might be better understood if we take this如果我们把这个描述可能会更好地理解

extern void
transpose (int N, int M, double out[M][N], const double in[N][M]);
double x[3][2];
double y[2][3];
…
transpose(3, 2, y, x);

Observing the above is that if you had an观察以上是,如果你有一个

s1=createStudent(s1, 123, "Poli");
s2=createStudent(s2, 456, "Rola);

Whereas the const char[5] for both "Poli" and "Rola" have correlation to a char a[].而“Poli”和“Rola”的 const char[5] 都与 char a[] 相关。 It is strictly not permitted as each element has the qualifier attached and not the entire array as a const.这是严格不允许的,因为每个元素都附加了限定符,而不是整个数组作为常量。

Well is ptr (which you passed in as void*) actually const or not?好吧,ptr(你作为 void* 传入)实际上是不是 const ? (In other words, is the memory under your control?) If it's not, then cast it to char* instead of const char* when calling strstr. (换句话说,内存是否在您的控制之下?)如果不是,则在调用 strstr 时将其转换为 char* 而不是 const char*。 If it is, however, you'll get a const char* out (pointing to a location inside of the string pointed to by ptr), and will then need to strncpy out to another string which you are responsible for managing.但是,如果是这样,您将得到一个 const char* 输出(指向 ptr 指向的字符串内部的位置),然后需要将 strncpy 输出到您负责管理的另一个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM