Write a program that defines an integer variable nNum and a
Write a program that defines
an integer variable nNum and a
pointer variable pnNum that points to the address of the integer
nNum.
Define two constant string variables sznNum = “nNum” and
szpnNum = “pnNum”. Your program should ask a user to enter an
integer for nNum. You should then ask the user to enter a
second
integer. You must use the pointer pnNum to change the value of
nNum. Be sure to display the address and value of nNum, the
address to which pnNum points, and the value of nNum after being
changed by the pointer. When displaying values, use the
string
variable references sznNum and szpnNum whenever you need to
display the variable names nNum and pnNum.
Solution
// Example program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int nNum;
int *pnNum=&nNum;
const string sznNum=\"nNum\";
const string szpnNum=\"pnNum\";
cout << \"Please enter an integer value: \";
cin >> nNum;
cout<<\"Value of \"+ sznNum +\" is \" << nNum << \"\ \";
cout<<\"Address of \"+ sznNum +\" is \" << &nNum << \"\ \";
cout<<\"Adress to which \"+ szpnNum +\" points is \" << pnNum << \"\ \";
cout << \"Please enter a second integer value: \";
cin >> *pnNum;
cout<<\"Value of \"+ sznNum +\" after being changed by pointer is \" << nNum << \"\ \";
}
