C++ 不能用'char' 类型的右值初始化'char *' 类型的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29270202/
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
C++ cannot initialize a variable of type 'char *' with an rvalue of type 'char'
提问by Haoran Jia
char *p = 's';
It gives out the error
它给出了错误
cannot initialize a variable of type '
char *
' with an rvalue of type 'char
'
不能初始化类型的“一个变量
char *
”类型的右值“char
”
Can anyone explain it to me? Thanks a lot.
谁能给我解释一下?非常感谢。
回答by Mike Seymour
p
is a pointer, of type char*
. 's'
is a character literal of type char
. You can't initialise a pointer from a character.
p
是一个指针,类型为char*
。's'
是类型为 的字符文字char
。您不能从字符初始化指针。
Maybe you want p
to be a single character:
也许你想p
成为一个单一的角色:
char p = 's';
or maybe you want it to point to a string containing the character 's'
:
或者您可能希望它指向包含字符的字符串's'
:
char const *p = "s"; // Must be const, since string literals are constant
回答by Joe Plante
Be thankful for your compiler. This might have worked in DOS and caused you major headaches.
感谢你的编译器。这可能在 DOS 中有效,并让您头疼不已。
You know how pointers are memory addresses? That's part of the issue. 's' can gets translated to a number in many C and/or C++ compilers. See an ASCII table
你知道指针是内存地址吗?这是问题的一部分。's' 可以在许多 C 和/或 C++ 编译器中转换为数字。查看 ASCII 表
char *p = 's'; can literally sets it p memory address 0x73, or whatever the OS declares the memory address to 0x73. The compiler is going "You can't do that!"
字符 *p = 's'; 可以字面上将其设置为 p 内存地址 0x73,或者操作系统将内存地址声明为 0x73 的任何内容。编译器会说“你不能那样做!”
In some forms of DOS and the right C compiler (no OS protections), you could actually do that and peek into/change whatever was stored at memory address 0x73. See http://webspace.webring.com/people/su/um_11214/vga.htmlfor why.
在某些形式的 DOS 和正确的 C 编译器(没有操作系统保护)中,您实际上可以这样做并查看/更改存储在内存地址 0x73 的任何内容。请参阅http://webspace.webring.com/people/su/um_11214/vga.html了解原因。
You could actually mess around your video card's RAM directly in DOS just by using a pointer. This is a huge reason why in the early days of Windows, many games still came out for DOS. It was a powerful technique
实际上,您可以通过使用指针直接在 DOS 中处理视频卡的 RAM。这就是为什么在 Windows 的早期,许多游戏仍然为 DOS 推出的一个重要原因。这是一个强大的技术
回答by ANjaNA
you made a mistake. You should use double quotation("
) instead of single quotation('
).
Typically, 's'
means a character literal and it is evaluated to type char
.
你犯了一个错误。您应该使用双引号 ( "
) 而不是单引号 ( '
)。通常,'s'
表示一个字符文字,它被评估为 type char
。
while your p
is a char
type pointer(char*
) initialization doesn't work.
而你的p
is a char
type pointer( char*
) 初始化不起作用。
use "s"
to get char pointer. Not that is gives you a const char *
and you can type case it to a char*
.
用于"s"
获取字符指针。这不是给你 aconst char *
并且你可以将它输入到 a char*
。
enter code here
char* p = (char*)"s";
or
或者
const char* p = "s"
回答by paxdiablo
It's because 's'
is indeed a char
type. You'll need one of the following:
因为's'
确实是一种char
类型。您将需要以下其中一项:
char p = 's';
const char *p = "s";
Use the first if you really only want to manipulate a single character.
如果您真的只想操作单个字符,请使用第一个。
Use the second if you want a C-style string.
如果你想要一个 C 风格的字符串,请使用第二个。