Write a program that definesan integer variable nNum and apointer variable pnNum
ID: 3776268 • Letter: W
Question
Write a program that definesan integer variable nNum and apointer variable pnNum that points to the address of the integernNum.Define two constant string variables sznNum = “nNum” andszpnNum = “pnNum”. Your program should ask a user to enter aninteger for nNum. You should then ask the user to enter asecondinteger. You must use the pointer pnNum to change the value ofnNum. Be sure to display the address and value of nNum, theaddress to which pnNum points, and the value of nNum after beingchanged by the pointer. When displaying values, use thestringvariable references sznNum and szpnNum whenever you need todisplay the variable names nNum and pnNum
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
int nNum,*pnNum,num2;
string sznNum = "nNum",szpnNum = "pnNum";
pnNum = &nNum;//assigning address to pointer
cout<<"Enter number1:";//taking user input
cin>>nNum;
cout<<"Enter number2:";
cin>>num2;
cout<<"Address of "<<sznNum<<" : "<<&nNum<<" "<<"Pointer "<<szpnNum<<" points to the address :"<<pnNum<<" ";
cout<<"Changing value of "<<sznNum<<" using pointer "<<szpnNum<<" .. ";
*pnNum = num2;//changing value using pointer
cout<<"Changed value of "<<sznNum<<" using pointer "<<szpnNum<<" is : "<<nNum<<" ";
return 0;
}
ouput::-
Enter number1:3
Enter number2:14
Address of nNum : 0x24fd70
Pointer pnNum points to the address :0x24fd70
Changing value of nNum using pointer pnNum ..
Changed value of nNum using pointer pnNum is : 14
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.