实例 C++ 使用解引用运算符 * 获取变量的值

x
 
#include <iostream>
#include <string>
using namespace std;
int main() {
  string food = "Pizza";  // Variable declaration
  string* ptr = &food;    // Pointer declaration
  // Reference: Output the memory address of food with the pointer
  cout << ptr << "\n";
  // Dereference: Output the value of food with the pointer
  cout << *ptr << "\n";
  return 0;
}
                    

输出结果

0x6dfed4
Pizza