请解释这个c ++程序是如何工作的?

时间:2017-04-16 16:24:24

标签: c++

我遇到麻烦,理解这个程序请帮助:

 #include <iostream>
 using namespace std;
 int main(){
 const char* s = 5+"hellow world";
 cout<<s;
 return 0;
 }

这是正确的,并提供以下输出enter image description here

1 个答案:

答案 0 :(得分:2)

在代码的第三行中,编译器会创建一个匿名字符数组。将5添加到c-string时,它会执行指针运算并将指针5向前移动到字符串。因此,它会跳过c-string中的5字符,并仅将数组中的其他字符存储到s中。

相关问题