#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
string s("hello world!");
string *sp = &s;
cout << s << endl;
cout << *sp << endl;
cout << sp << endl;
return 0;
}
结果:
针对以上的程序,我们最开始定义了一个s字符串变量,这个变量中赋值为hello world!,接下来我定义了一个指针sp,这个sp是用来存放字符串变量s的地址(&s),那么在sp前面的*代表的意思就是这个sp变量是一个指针变量,这个指针是一个指向字符串的指针。
但是在 cout << *sp << endl;此处,sp代表一个指针,但是*代表的意思是解引用,意思就是将指针所指向的地址中存放的值给取出来,因此其结果依然是hello,world!。
在cout << sp << endl;中sp是一个指针,也就是地址,因此输出的值是0018FF2C,代表地址信息。