- Get link
- X
- Other Apps
#include <iostream>
#include <string>
using namespace std;
int main() {
string food = "Pizza"; // A string variable
string* ptr = &food; // A pointer variable that stores the address of food
// Output the value of food
cout << food << "\n"; // Pizza
// Output the memory address of food
cout << &food << "\n"; // 0x7ffe17728500
// Output the memory address of food with the pointer
cout << ptr << "\n"; // 0x7ffe17728500
// Output the value in the memory address which is stored in ptr
cout << *ptr << "\n"; // Pizza
// 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;
}
#include <string>
using namespace std;
int main() {
string food = "Pizza"; // A string variable
string* ptr = &food; // A pointer variable that stores the address of food
// Output the value of food
cout << food << "\n"; // Pizza
// Output the memory address of food
cout << &food << "\n"; // 0x7ffe17728500
// Output the memory address of food with the pointer
cout << ptr << "\n"; // 0x7ffe17728500
// Output the value in the memory address which is stored in ptr
cout << *ptr << "\n"; // Pizza
// 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;
}
- Get link
- X
- Other Apps
Comments
Post a Comment