C++ 如何将 const char* 转换为 char*
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/833034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert const char* to char*
提问by Cute
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] = 'if (NULL != strstr((const char *)ptr, "550"))
{
';
error->ret = true;
}
return size*nmemb;
}
采纳答案by Daniel Earwicker
You don't appear to use err
in the rest of that function, so why bother creating it?
您似乎没有err
在该函数的其余部分使用,那为什么还要创建它呢?
const char* err = 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
:
if (NULL != strstr(reinterpret_cast<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:
最后,由于强制转换是如此令人讨厌的事情,因此最好对要执行的操作使用特定的现代风格强制转换。在这种情况下:
char *
strstr(const char *s1, const char *s2);
回答by Jared Oberhaus
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. In my experiments I made the return type size_t
.
您提供的代码无法编译;get_error_from_header
不指定返回类型。在我的实验中,我做了返回类型size_t
。
Signature of C++ strstr()
C++的签名 strstr()
The signature for strstr()
in the standard C library is:
strstr()
标准 C 库中的签名是:
const char * strstr ( const char * str1, const char * str2 );
char * strstr ( char * str1, const char * str2 );
but the signature for strstr()
in the C++ library, depending on the overload, is one of:
但对于签名strstr()
在C ++库,这取决于过载,是以下之一:
const char* err = strstr((const char *)ptr, "550");
if (err != NULL) {
...
}
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:
我会选择第一个重载,因为你不想修改字符串,你只想读取它。因此,您可以将代码更改为:
//error cannot convert const char** to char*
Also, I'm assuming your comment reporting the error:
另外,我假设您的评论报告了错误:
if (strstr((const char *)ptr, "550") != NULL) {
...
}
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
. Therefore you could use:
正如在之前的答案中指出的那样,如果使用err
来存储 的结果strstr
只是检查,则没有必要使用它NULL
。因此,您可以使用:
if (strstr(reinterpret_cast<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 风格的强制转换:
const char * p1;
char * p2;
p2 = const_cast<char *>(p1);
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. As in:
鉴于问题中的示例,我看不出这有什么必要,但是如果您有一个需要const
去除 -ness的变量,则应该使用const_cast<>
运算符。如:
char* err = strstr((char *)ptr,"550");
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.
正如评论中所指出的,使用const_cast<>
operator的原因是为了让作者的意图明确,也为了便于搜索使用const_cast<>
; 通常剥离 const 是错误或设计缺陷的来源。
回答by Matthew Flaschen
Can't you just do:
你不能只做:
const char* mstr="";
char* str=const_cast<char*>(mstr);
The error is because if you pass in a const char* to strstr you get one out (because of the overload).
错误是因为如果您将 const char* 传递给 strstr ,则会得到一个(因为过载)。
回答by hmehdi
//try this instead:
//试试这个:
##代码##回答by Yuliy
Well is ptr (which you passed in as void*) actually const or not? (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. 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.
好吧,ptr(你作为 void* 传入)实际上是不是 const ?(换句话说,内存是否在您的控制之下?)如果不是,则在调用 strstr 时将其转换为 char* 而不是 const char*。但是,如果是这样,您将得到一个 const char* 输出(指向 ptr 指向的字符串内部的位置),然后需要将 strncpy 输出到您负责管理的另一个字符串。