Указатели являются неотъемлемой частью языка C++. Это позволяет нам хранить адрес памяти переменной. Здесь следует отметить одну вещь: если мы изменим значение, на которое указывает указатель, то значение исходной переменной также изменится. Теперь давайте проверим подход, который мы собираемся использовать.
Подход:
В приведенном ниже примере мы сначала объявим и инициализируем строковую переменную. Затем мы сохраним адрес строковой переменной в указателе. Наконец, мы изменим значение, на которое указывает указатель, и выведем на экран все значения, связанные с программой.
Пример:
С++
// C++ program to demonstrate the
// process of modifying a pointer
#include <iostream>
#include <string>
using
namespace
std;
int
main()
{
string website =
"ABC"
;
string* p = &website;
cout <<
"The value of the variable: "
<< website
<<
"\n"
;
cout <<
"The memory adress of the variable: "
<< p
<<
"\n"
;
cout <<
"The value that the pointer is pointing to: "
<< *p <<
"\n"
;
*p =
"GeeksforGeeks"
;
cout
<<
"The new value that the pointer is pointing to: "
<< *p <<
"\n"
;
cout <<
"The memory adress of the variable: "
<< p
<<
"\n"
;
cout <<
"The new value of the variable: "
<< website
<<
"\n"
;
return
0;
}
Выход
The value of the variable: ABC The memory adress of the variable: 0x7ffe8d063980 The value that the pointer is pointing to: ABC The new value that the pointer is pointing to: GeeksforGeeks The memory adress of the variable: 0x7ffe8d063980 The new value of the variable: GeeksforGeeks