如何将 const char* 转换为 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这里有一些事情我不明白。 我看到它被标记为 C++/CLI,但我所描述的下面应该与标准 C++ 相同。
无法编译
您给出的代码无法编译;
get_error_from_header
未指定返回类型。 在我的实验中,我设置了返回类型size_t
。C++
strstr()
的签名标准 C 库中
strstr()
的签名是:但是
strstr()
中的签名是: a href="http://www.cplusplus.com/reference/clibrary/cstring/strstr/" rel="noreferrer">C++ 库,根据重载,是其中之一:我会选择第一个重载,因为您不想修改字符串,只想读取它。 因此,您可以将代码更改为:
另外,我假设您报告错误的评论:
是一个拼写错误:没有
const char**
可见。不必要对
err
进行赋值正如之前的答案中指出的那样,如果使用了
err
的所有结果,则无需使用err
来存储strstr
的结果for 正在检查NULL
。 因此,您可以使用:鼓励使用
reinterpret_cast<>
正如另一个答案中指出的那样,您应该使用
reinterpret_cast<>
而不是 C 风格的强制转换:使用
const_cast<>
剥离const
鉴于问题中的示例,我不知道哪里有必要这样做,但是如果您有一个需要剥离的变量
const
-ness,您应该使用const_cast<>
运算符。 如:正如评论中指出的,之所以使用
const_cast<>
运算符,是为了让作者的意图明确,同时也是为了方便搜索的使用>const_cast<>
; 通常,剥离 const 是错误或设计缺陷的根源。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 typesize_t
.Signature of C++
strstr()
The signature for
strstr()
in the standard C library is:but the signature for
strstr()
in the C++ library, depending on the overload, is one of: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:
Also, I'm assuming your comment reporting the error:
is a typo: there's no
const char**
to be seen.Assignment to
err
unnecessaryAs is pointed out in a previous answer, the use of
err
to store the result ofstrstr
is unnecessary if all it's used for is checkingNULL
. Therefore you could use:Use of
reinterpret_cast<>
encouragedAs is pointed out in another answer you should be using
reinterpret_cast<>
instead of C-style casts:Use of
const_cast<>
to stripconst
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 theconst_cast<>
operator. As in: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 ofconst_cast<>
; usually stripping const is the source of bugs or a design flaw.您似乎没有在该函数的其余部分中使用
err
,那么为什么还要创建它呢?如果您确实需要它,您真的需要修改它指向的任何内容吗? 如果不是,那么也将其声明为 const:
最后,由于强制转换是如此令人讨厌的事情,因此最好对要执行的操作使用特定的现代风格强制转换。 在这种情况下:
You don't appear to use
err
in the rest of that function, so why bother creating it?If you do need it, will you really need to modify whatever it points to? If not, then declare it as
const
also: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:
你不能这样做吗:
错误是因为如果你将 const char* 传递给 strstr 你会得到一个(因为过载)。
Can't you just do:
The error is because if you pass in a const char* to strstr you get one out (because of the overload).
//试试这个:
//try this instead:
那么 ptr (你作为 void* 传入的)实际上是 const 还是不是? (换句话说,内存是否在您的控制之下?)如果不是,则在调用 strstr 时将其强制转换为 char* 而不是 const char*。 但是,如果是这样,您将得到一个 const char* 输出(指向 ptr 所指向的字符串内部的位置),然后需要将 strncpy 输出到您负责管理的另一个字符串。
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.
根据我的深入研究,我发现许多论坛没有直接解决方案或对这个问题的参考答案,然后我深入研究 GCC 在线文档,简要阅读他们的编译器正确文档,这就是我可以提供的。
在 GNU C 中,指向带有限定符的数组的指针的工作方式与指向其他限定类型的指针类似。 例如,int ()[5] 类型的值可用于初始化 const int ()[5] 类型的变量。 然而,这些类型在 ISO C 中不兼容,因为 const 限定符正式附加到数组的元素类型,而不是数组本身。
如果我们采取这一点,这个描述可能会更好地理解。
观察上面的情况是,如果您有一个
“Poli”和“Rola”的 const char[5] 都与 char a[] 相关。 这是严格不允许的,因为每个元素都附加了限定符,而不是整个数组作为 const。
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.
In GNU C, pointers to arrays with qualifiers work similar to pointers to other qualified types. For example, a value of type int ()[5] can be used to initialize a variable of type 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.
This description might be better understood if we take this
Observing the above is that if you had an
Whereas the const char[5] for both "Poli" and "Rola" have correlation to a char a[]. It is strictly not permitted as each element has the qualifier attached and not the entire array as a const.