مقدار اشاره گر را تغییر دهید(Modify the Pointer Value)

همچنین می توانید مقدار اشاره گر را تغییر دهید. اما توجه داشته باشید که این نیز تغییر خواهد کرد
مقدار متغیر اصلی:



مثال



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";









تمرینات C++



خود را با تمرینات آزمایش کنید



ورزش:


یک متغیر نشانگر با نام ptr ایجاد کنید، که باید به یک رشته اشاره کند< /code> متغیر با نام غذا:



غذای رشته ای = "پیتزا";
= &;





شروع تمرین