C语言 如何在 C 中将 const char* 转换为 char*?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25549562/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:18:54  来源:igfitidea点击:

How to convert const char* to char* in C?

cpointersconst-char

提问by Morpheus

In my project there is a method which only returns a const char*, whereas I need a char*string, as the API doesn't accept const char*.

在我的项目中,有一个方法只返回一个const char*,而我需要一个char*字符串,因为 API 不接受const char*.

Any idea how to convert between const char*to char*?

任何想法如何之间的转换const char*char*

采纳答案by meaning-matters

To be safe you don't break stuff (for example when these strings are changed in your code or further up), or crash you program (in case the returned string was literal for example like "hello I'm a literal string"and you start to edit it), make a copy of the returned string.

为安全起见,您不会破坏内容(例如,当这些字符串在您的代码中或进一步更改时),或使您的程序崩溃(如果返回的字符串是文字,例如"hello I'm a literal string"并且您开始编辑它),请创建一个返回的字符串的副本。

You could use strdup()for this, but read the small print. Or you can of course create your own version if it's not there on your platform.

您可以strdup()为此使用,但请阅读小字。或者,如果您的平台上没有它,您当然可以创建自己的版本。

And the question remains: Why do you need a char*, can't you change your code so that it works with const char*?

问题仍然存在:为什么你需要一个char*,你不能改变你的代码以便它与 一起工作const char*吗?

回答by Wojtek Surowka

First of all you should do such things only if it is really necessary - e.g. to use some old-style API with char*arguments which are not modified. If an API function modifies the string which was const originally, then this is unspecified behaviour, very likely crash.

首先,您应该仅在确实有必要时才执行此类操作 - 例如,使用一些带有char*未修改参数的旧式 API 。如果 API 函数修改了最初为 const 的字符串,则这是未指定的行为,很可能会崩溃。

Use cast:

使用演员表:

(char*)const_char_ptr

回答by j123b567

You can use the strdupfunction which has the following prototype

您可以使用strdup具有以下原型的函数

char *strdup(const char *s1);

Example of use:

使用示例:

#include <string.h>

char * my_str = strdup("My string literal!");
char * my_other_str = strdup(some_const_str);

or strcpy/strncpy to your buffer

或 strcpy/strncpy 到您的缓冲区

or rewrite your functions to use const char *as parameter instead of char *where possible so you can preserve the const

或重写您的函数以const char *用作参数而不是char *在可能的情况下使用,以便您可以保留const

回答by raghav3276

A constto a pointer indicates a "read-only" memory location. Whereas the ones without constare a read-write memory areas. So, you "cannot" convert a const(read-only location) to a normal(read-write) location.

const指向指针的A表示“只读”内存位置。而没有的const是读写存储区。因此,您“无法”将const(只读位置)转换为正常(读写)位置。

The alternate is to copy the data to a different read-write location and pass this pointer to the required function. You may use strdup()to perform this action.

另一种方法是将数据复制到不同的读写位置并将此指针传递给所需的函数。您可以使用strdup()来执行此操作。

回答by Bikash

To convert a const char*to char*you could create a function like this :

要将 a 转换const char*char*您可以创建这样的函数:

#include <stdio.h>

char* unconstchar(const char* s) {
    int i;
    char* res;
    for (i = 0; s[i] != '##代码##'; i++) {
        res[i] = s[i];
    }
    res[i] = '##代码##';
    return res;
}

int main() {
    const char* s = "hi this is bikash\n";
    char* p = unconstchar(s);
    printf("%s",p);
}

回答by dhein

You can cast it by doing (char *)Identifier_Of_Const_char

你可以通过做来投射它 (char *)Identifier_Of_Const_char

But as there is probabbly a reason that the api doesn't accept const cahr *, you should do this only, if you are sure, the function doesn't try to assign any value in range of your const char*which you casted to a non const one.

但是,可能有一个 api 不接受的原因const cahr *,您应该只这样做,如果您确定,该函数不会尝试在const char*您强制转换为非 const 的范围内分配任何值。