C++ 将1添加到字符串文字的语句中的差异
我一直在做下面的作业来解释3个语句中发生了什么,但我想不出来C++ 将1添加到字符串文字的语句中的差异,c++,string,C++,String,我一直在做下面的作业来解释3个语句中发生了什么,但我想不出来 cout << ("hello" + 1); // ello cout << (*"hello") + 1; // 105 cout << (*("hello" + 1)); // e cout 您可以在此处取消引用一个const char[]。第一个字符是h,带有ascii码104。添加一个,您将获得105 cout << (*("hello" + 1)); // e c
cout << ("hello" + 1); // ello
cout << (*"hello") + 1; // 105
cout << (*("hello" + 1)); // e
cout
您可以在此处取消引用一个const char[]
。第一个字符是h,带有ascii码104
。添加一个,您将获得105
cout << (*("hello" + 1)); // e
cout“hello”是const char*
为什么2是数字->*“hello”将是基址处的值,即h(104)的ascii值,所以104+1=105
是的,您刚才指向的是e
,而不是h
*“hello”
给出字符串的第一个字符'h'
,类型为char
,ASCII值为104。整数提升规则意味着,当添加char
和int
时,char
转换为int
,给出类型为int
的结果。输出int
给出数值
对。字符串文字是以零字符结尾的数组。将一个添加到其地址会给出指向数组第二个字符的指针;数组的其余部分不变,因此在末尾仍然包含零
值得注意的是,字符串文字是const char[N]
s。不能取消对数组的引用。您取消引用数组衰减成的指针。“hello”
是const char[6]
@chris string我猜是文本,所以它可以是const char*,但是const char[6]
?是的,您可以通过std::is_same
自己看到。
cout << (*"hello") + 1; // 105
cout << (*("hello" + 1)); // e