我在教自己C++,我对指针有点困惑(特别是在下面的源代码中).但首先,我继续向你展示我所知道的内容(然后将代码与此对比,因为我觉得好像存在一些矛盾).
我知道的:
int Age = 30;
int* pointer = &Age;
cout << "The location of variable Age is: " << pointer << endl;
cout << "The value stored in this location is: " << *pointer << endl;
Run Code Online (Sandbox Code Playgroud)
指针保存内存地址.使用间接(取消引用)运算符(*),可以访问存储在指针的内存位置的内容.在本书的代码中,我无法理解......
cout << "Enter your name: ";
string name;
getline(cin, name); //gets full line up to NULL terminating character
int CharsToAllocate = name.length() + 1; //calculates length of string input
//adds one onto it to adjust for NULL character
char* CopyOfName = new …
Run Code Online (Sandbox Code Playgroud)