实例 C++ 修改指针的值

x
 
#include <iostream>
#include <string>
using namespace std;
int main() {
  string food = "Pizza";
  string* ptr = &food;
  // Output the value of food
  cout << food << "\n";
  // Output the memory address of food
  cout << &food << "\n";
  // Access the memory address of food and output its value
  cout << *ptr << "\n";
  
  // Change the value of the pointer
  *ptr = "Hamburger";
  
  // Output the new value of the pointer
  cout << *ptr << "\n";
  
  // Output the new value of the food variable
  cout << food << "\n";
  return 0;
}
                    

输出结果

Pizza
0x6dfed4
Pizza
Hamburger
Hamburger