Jump to content

C++ Lessons : 21.2. Modify Pointer Value (FINAL LESSON)


quix
 Share

Recommended Posts

  • Manager

Modify the Pointer Value

You can also change the pointer's value. But note that this will also change the value of the original variable:

Example

string food = "Pizza";
string* ptr = &food;

// Output the value of food (Pizza)
cout << food << "\n";

// Output the memory address of food (0x6dfed4)
cout << &food << "\n";

// Access the memory address of food and output its value (Pizza)
cout << *ptr << "\n";

// Change the value of the pointer
*ptr = "Hamburger";

// Output the new value of the pointer (Hamburger)
cout << *ptr << "\n";

// Output the new value of the food variable (Hamburger)
cout << food << "\n";

 

 

Thank you for being part of my journey to teaching you all the C++ Basics you'll need to start programming! Have fun coding 😄

Hi,

DM me ONLY if you have problems on my uploads!

With love, quix!

Link to comment
Share on other sites

  • quix locked this topic
Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...