Help with Pointers

Can you please tell me if i have done it correctly.


Introduce int variables x and y and int* pointer variables p and q. Set x to 2, y to 8, p to the address of x, and q to the address of y. Then print the following information:
(1) The address of x and the value of x.
(2) The value of p and the value of *p.
(3) The address of y and the value of y.
(4) The value of q and the value of *q.
(5) The address of p (not its contents!).
(6) The address of q (not its contents!).
Print all pointer/address values and format the output so it is easy to make comparisons.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;

void main()
{
	int x,y;
	int *p,*q;
	x=2;
	y=8;
	p=&x;
	q=&y;
	
	cout<<"The address of x is "<<&x<<" and the value of x is "<<x<<endl;
	cout<<"The value of p is "<<p<<" and the value of *p is "<<*p<<endl;
	cout<<"The address of y is "<<&y<<" and the value of y is "<<y<<endl;
	cout<<"The value of q is "<<q<<" and the value of *q "<<*q<<endl;
	cout<<"The address of p (not its contents!) is "<<&p<<endl;
	cout<<"The address of q (not its contents!) is "<<&q<<endl;

	//Print all pointer/address values and format the output so it is easy to make comparisons.


}//end main 

It is correct, but you should declare main as int main() instead of void main().
Topic archived. No new replies allowed.