如何将 const char* 转换为 char*

发布于 2024-07-20 13:30:45 字数 480 浏览 10 评论 0原文

任何人都可以告诉我如何将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

阳光的暖冬 2024-07-27 13:30:45

这里有一些事情我不明白。 我看到它被标记为 C++/CLI,但我所描述的下面应该与标准 C++ 相同。

无法编译

您给出的代码无法编译; get_error_from_header 未指定返回类型。 在我的实验中,我设置了返回类型 size_t

C++ strstr() 的签名

标准 C 库中 strstr() 的签名是:

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

但是 strstr() 中的签名是: a href="http://www.cplusplus.com/reference/clibrary/cstring/strstr/" rel="noreferrer">C++ 库,根据重载,是其中之一:

const char * strstr ( const char * str1, const char * str2 );
      char * strstr (       char * str1, const char * str2 );

我会选择第一个重载,因为您不想修改字符串,只想读取它。 因此,您可以将代码更改为:

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

另外,我假设您报告错误的评论:

//error  cannot convert const char** to char*

是一个拼写错误:没有 const char** 可见。

不必要对 err 进行赋值

正如之前的答案中指出的那样,如果使用了 err 的所有结果,则无需使用 err 来存储 strstr 的结果for 正在检查NULL。 因此,您可以使用:

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

鼓励使用 reinterpret_cast<>

正如另一个答案中指出的那样,您应该使用 reinterpret_cast<> 而不是 C 风格的强制转换:

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

使用const_cast<> 剥离 const

鉴于问题中的示例,我不知道哪里有必要这样做,但是如果您有一个需要剥离的变量const-ness,您应该使用 const_cast<> 运算符。 如:

const char * p1;
char * p2;

p2 = const_cast<char *>(p1);

正如评论中指出的,之所以使用 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 type size_t.

Signature of C++ 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.

Assignment to err unnecessary

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:

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

Use of reinterpret_cast<> encouraged

As 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) {
    ...
}

Use of 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.

久夏青 2024-07-27 13:30:45

您似乎没有在该函数的其余部分中使用 err ,那么为什么还要创建它呢?

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

如果您确实需要它,您真的需要修改它指向的任何内容吗? 如果不是,那么也将其声明为 const:

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

最后,由于强制转换是如此令人讨厌的事情,因此最好对要执行的操作使用特定的现代风格强制转换。 在这种情况下:

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

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

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

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"))
{
飘落散花 2024-07-27 13:30:45

你不能这样做吗:

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

错误是因为如果你将 const char* 传递给 strstr 你会得到一个(因为过载)。

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).

黯淡〆 2024-07-27 13:30:45

//试试这个:

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

//try this instead:

const char* mstr="";
char* str=const_cast<char*>(mstr);
一梦浮鱼 2024-07-27 13:30:45

那么 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.

疯到世界奔溃 2024-07-27 13:30:45

根据我的深入研究,我发现许多论坛没有直接解决方案或对这个问题的参考答案,然后我深入研究 GCC 在线文档,简要阅读他们的编译器正确文档,这就是我可以提供的。

在 GNU C 中,指向带有限定符的数组的指针的工作方式与指向其他限定类型的指针类似。 例如,int ()[5] 类型的值可用于初始化 const int ()[5] 类型的变量。 然而,这些类型在 ISO C 中不兼容,因为 const 限定符正式附加到数组的元素类型,而不是数组本身。

如果我们采取这一点,这个描述可能会更好地理解。

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

观察上面的情况是,如果您有一个

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

“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

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[]. It is strictly not permitted as each element has the qualifier attached and not the entire array as a const.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文