Exercise 3 (20 points): Define the following variables (in a new file and a new
ID: 667396 • Letter: E
Question
Exercise 3 (20 points):
Define the following variables (in a new file and a new main() function):
• Integer value a and b
• Pointer variable p and q (pointers to integer values)
• Set the value of a to 5 and value of b to 7
• Set p to point to a and q to point to b (in other words assign the address of variables to pointers)
a) Use cout to print the following information:
(1) The address of a and the value of a.
(2) The value of p and the value of *p.
(3) The address of p.
(4) The address of b and the value of b.
(5) The value of q and the value of *q.
(6) The address of q.
*** C++ PROGRAMMING
Explanation / Answer
#include <iostream>
using namespace std;
int main ()
{
int a = 5, b = 7;
int * p, * q;
p = &a; // p = address of a
q = &b; // q = address of b
cout << "address of a and value of a is " << &a ; a << ' ';
cout << "value of p and *p is" << p ; *p << ' ';
cout<<"address of p is"<< &p << ' ';
cout << "address of b and value of b is " << &b ; b << ' ';
cout << "value of q and *q is" << q ; *q << ' ';
cout<<"address of q is"<< &q << ' ';
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.