Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 这两者在c++;_C++ - Fatal编程技术网

C++ 这两者在c++;

C++ 这两者在c++;,c++,C++,对于第一个,我在Visual Studio IDE上没有看到任何错误消息,但是对于第二个,我得到了以下错误消息: 错误C2440:“正在初始化”:无法从“常量字符[2]”转换为“字符” 有可能知道两者之间的区别吗? 如果它们不同,它们之间有什么不同的属性 我在读到,如果你有双引号,那么它是一个字符串文字,但如果你有一个单引号,那么它是一个字符 我还读到,如果符号为[],字符y1[]被视为一个数组 我有点对上述内容感到困惑。请有人解释一下,并给出一些简单明了的例子来澄清 此外,如果上述情况确实不同

对于第一个,我在Visual Studio IDE上没有看到任何错误消息,但是对于第二个,我得到了以下错误消息: 错误C2440:“正在初始化”:无法从“常量字符[2]”转换为“字符”

有可能知道两者之间的区别吗? 如果它们不同,它们之间有什么不同的属性

我在读到,如果你有双引号,那么它是一个字符串文字,但如果你有一个单引号,那么它是一个字符

我还读到,如果符号为[],字符y1[]被视为一个数组

我有点对上述内容感到困惑。请有人解释一下,并给出一些简单明了的例子来澄清

此外,如果上述情况确实不同,那么以下情况如何:

char y1[] = "a";

char y2 = "a";
< > >代码“A”/>代码>这是C++中和C中的“强>字符串字符串< /强>的示例。它用于存储许多字符,如<代码>”ABCDEG“< /COD>”。p> 字符串文本的最后一个字符总是
\0
,这是字符串的经典C定义

正如您已经提到的,
chary1[]
表示一个字符数组。这可以指定给字符串文字。e、 g

chary1[]=“abcd”

在这种情况下,
y1
被定义为具有5个字符的大小,并且在末尾具有
\0
。原始C字符串
“abcd”
被复制到数组中

char y2=“a”

不能将字符串文字指定给单个字符,因为末尾有
'\0


< C++ >代码>是C++和C中的<强>字符文字< /强>,用于存储单个字符。

字符y1[]='a'

这是不允许的,因为您正在为数组指定单个字符文字

字符y1[]=“a”;/*这是双引号*/

如上所示,这是允许的

字符y2='a'

您正在将字符文字指定给单个字符变量,因此这是正常的

char y2=“a”/*这是双引号,没有[]*/


这是不正确的,因为您正在为字符变量指定字符串文字。C++中没有结束空间<代码> 0

< P>。首先,与其处理原始字符串,不如使用字符串对象(例如)

示例一:

char y1[]='a';
char y1[]="a"; /* this is double quotation*/

char y2 = 'a';   
char y2 = "a"; /* this is double quotation and without the []*/
char y1 = 'a';
char y1[] = 'a'; // error: array initializer must be an initializer list or string literal
char y1[]="a";
char y1 = "a"; // cannot initialize a variable of type 'char' with an lvalue of type 'const char [2]
这称为单字符文字,按照惯例,它是使用单引号定义的。然后分配相应的十六进制值

示例二:

char y1[]='a';
char y1[]="a"; /* this is double quotation*/

char y2 = 'a';   
char y2 = "a"; /* this is double quotation and without the []*/
char y1 = 'a';
char y1[] = 'a'; // error: array initializer must be an initializer list or string literal
char y1[]="a";
char y1 = "a"; // cannot initialize a variable of type 'char' with an lvalue of type 'const char [2]
这将导致编译错误,因为
y1
被声明为数组,其大小尚未定义,因为单字符文字无法初始化数组

示例三:

char y1[]='a';
char y1[]="a"; /* this is double quotation*/

char y2 = 'a';   
char y2 = "a"; /* this is double quotation and without the []*/
char y1 = 'a';
char y1[] = 'a'; // error: array initializer must be an initializer list or string literal
char y1[]="a";
char y1 = "a"; // cannot initialize a variable of type 'char' with an lvalue of type 'const char [2]
本例在(本地)堆栈上声明一个字符串数组
y1
。数组大小为2,以
\0
终止。此数组可以修改,并且在程序离开声明此数组的作用域后将被删除

示例四:

char y1[]='a';
char y1[]="a"; /* this is double quotation*/

char y2 = 'a';   
char y2 = "a"; /* this is double quotation and without the []*/
char y1 = 'a';
char y1[] = 'a'; // error: array initializer must be an initializer list or string literal
char y1[]="a";
char y1 = "a"; // cannot initialize a variable of type 'char' with an lvalue of type 'const char [2]
此示例将导致编译器错误。原因是在这种情况下,
“a”
确实以
\0
隐式终止(因此长度为2),但
y1
只能存储1个字符

示例五:

char y1[]='a';
char y1[]="a"; /* this is double quotation*/

char y2 = 'a';   
char y2 = "a"; /* this is double quotation and without the []*/
char y1 = 'a';
char y1[] = 'a'; // error: array initializer must be an initializer list or string literal
char y1[]="a";
char y1 = "a"; // cannot initialize a variable of type 'char' with an lvalue of type 'const char [2]
这是声明和定义常量字符串字符数组的另一种正确方法。在这种情况下,数组是常量,不能修改(这对于标识符或您不想修改的其他字符串数组很有帮助)


您也可以找到这方面的一个很好的总结

您好,在您的示例III中,您说过“…不是以\0结尾的”??对不起,您的意思是它以\0结尾吗?因为从上面的另一个人的解释来看,字符串文字(即双引号中的内容)的结尾似乎总是以\0结尾。@john\u如果您是正确的,示例III中有一个错误:字符串文字总是以空字符结尾,因此
“a”
是一个两个字符的字符串。您也可以通过以下事实看到这一点:
字符a[1]=“a”生成编译错误。在VS2019中,错误状态为:
类型为“const char[2]”的值不能用于初始化类型为“char[1]”的实体。
它是用于单引号字符的句子。我修正了这个例子