C++ “const char*”类型的值不能用于初始化“char *”类型的实体

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

A value of type "const char*" cannot be used to initialize an entity of type "char *"

c++

提问by nTIAO

I have a code like this but I keep receiving this error :
A value of type "const char*" cannot be used to initialize an entity of type "char *".
What is going on?
I have read up on the following threads but have not been able to seen any result to my answer as all of them are either from charto char*or char*to char:
Value type const char cannot be used to initialize an entity of type char*
Value of type char* cannot be used to initialize an entity of type "char"

我有这样的代码,但我一直收到此错误:
“const char*”类型的值不能用于初始化“char *”类型的实体。
到底是怎么回事?
我已经阅读了以下线程,但无法看到我的答案的任何结果,因为它们都来自chartochar*char*to char
值类型 const char 不能用于初始化 char* 类型的实体 char*
类型的值* 不能用于初始化“char”类型的实体

#include <iostream>;
using namespace std;

int main() {
    int x = 0; //variable x created
    int cars (14);//cars is created as a variable with value 14
    int debt{ -1000 };//debt created with value 1000
    float cash = 2.32;
    double credit = 32.32;
    char a = 'a';//for char you must use a single quote and not double
    char* sandwich = "ham";
    return 0;
}

EDIT: I am using visual studio community 2017

编辑:我正在使用 Visual Studio 社区 2017

采纳答案by Davislor

That is correct. Let's say you had the following code:

那是正确的。假设您有以下代码:

const char hello[] = "hello, world!";
char* jello = hello; // Not allowed, because:
jello[0] = 'J'; // Undefined behavior!

Whoops! A const char*is a non-const pointer to const char. If you assign its value to a non-const char*, you've lost its constproperty.

哎呀!Aconst char*是指向 的非常量指针const char。如果你将它的值赋给一个非常量char*,你就失去了它的const属性。

A constpointer tonon-const charwould be a char* const, and you can initialize a char*from that all day if you want.

一个const指向非constchar将是一个char* const,你可以初始化char*从一整天,如果你想要的。

You can, if you really want, achieve this with const_cast<char*>(p), and I occasionally have, but it's usually a sign of a serious design flaw. If you actually get the compiler to emit instructions to write to the memory aliased by a string constant, you get undefined behavior. One of the many things that mightgo wrong is that some implementations will store the constant in read-only memory and crash. Or the same bytes of memory might be re-used for more than one purpose, because after all, we warned you never to change it.

如果您真的想要,您可以使用 实现这一点const_cast<char*>(p),我偶尔会这样做,但这通常表明存在严重的设计缺陷。如果您确实让编译器发出指令以写入由字符串常量作为别名的内存,则会出现未定义的行为。许多可能出错的事情之一是某些实现会将常量存储在只读内存中并崩溃。或者相同字节的内存可能会被重复使用不止一个目的,因为毕竟我们警告过你永远不要改变它。

By the way, the rules in C are different. This is solely for backward-compatibility with early versions of C that did not have the constkeyword, and you should never write new code that uses a non-const alias to a string constant.

顺便说一下,C 中的规则是不同的。这仅仅是为了与没有const关键字的早期版本的 C 向后兼容,并且您永远不应该编写使用非常量别名到字符串常量的新代码。

回答by Justin Randall

You need to make your string literal type constbecause in C++ it is a constant array of char, unlike C where it is just an array of char. You cannot change a string literal, so making it constis preferred in C++ for extra safety. It is the same reason you have to use an explicit cast when going from const char*to char*. It's still technically "allowed" in C++ since it is allowed in C, which is why it's just a warning. It's still bad practice to do so. To fix the warning, make it const.

您需要创建字符串文字类型,const因为在 C++ 中它是 的常量数组char,而 C 中它只是char. 您不能更改字符串文字,因此const在 C++ 中首选它以增加安全性。这与从const char*to 转到时必须使用显式强制转换的原因相同char*。它在 C++ 中在技术上仍然是“允许的”,因为它在 C 中是允许的,这就是为什么它只是一个警告。这样做仍然是不好的做法。要修复警告,请使其const.

const char* sandwich = "ham";