下面是我想知道旁边注释中2个问题的答案的代码。请帮我C++中的字符串和指针
#include<iostream>
using namespace std;
int main()
{
char *p="Hello";
cout <<*p; //gives H
cout <<*(p++); //also gives H.Why?
cout <<*(p++); //gives e.
cout <<*(p++); //gives l.
cout <<*(p++); //gives l.
cout <<*(p++); //gives o.
cout <<*(p++); //gives no output.Why? It should give some garbage value!
}
使'const char *',而不是'char *'。 – chris
@chris:这是一个不同的问题。我想了解当前代码的奇怪输出。 –
p ++表示后增量,取第一个值,然后递增 –