Thursday, May 21, 2020

Modify Pointer Value in C++


  • Program :

#include <iostream>
#include <string>
using namespace std;
int main(){
  cout <<"\n----------------POINTERS----------------\n";
  cout <<"\n----------------Modify Pointer Value----------------\n";
   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 = "burger";
 
  // Output the new value of the pointer
  cout << *ptr << "\n";
 
  // Output the new value of the food variable
  cout << food << "\n";
  return 0;
}

  • Output :

No comments:

Post a Comment

Function Overloading in C++

https://monitechvenkat1620.blogspot.com Program : #include <iostream> using namespace std; int c(int x, int y) {   return...