我在教自己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) 我无法理解C风格的字符串是什么.新年快乐
我所知道的:指针包含一个内存地址.取消引用指针将为您提供该内存位置的数据.
int x = 50;
int* ptr = &x; //pointer to an integer, holds memory address of x
cout << "&x: " << &x << endl; //these two lines give the same output as expected
cout << "ptr: " << ptr << endl;
cout << "*ptr: " << dec << (*ptr) << endl; //prints out decimal number 50
//added dec, so the program doesnt
//continue to printout hexidecimal numbers like it did for the
//the memory addresses above
cout …Run Code Online (Sandbox Code Playgroud)