Comparison

I want to compare the memory address and pointer value of p, p + 1, q , and q + 1.
I want to understant what the following values actually mean. I can't quite wrap my head around whats going on.
When I run the code

I Get An Answer Of 00EFF680 for everytime I compare the adresss p with another pointer.
I Get An Answer of 00EFF670 for everytime I compare the address of q with another pointer

I Get An Answer of 15726208 when I look at the pointer value of p and I Get An Answer of 15726212 When I look at the pointer value of p + 1.

I Get An Answer of 15726192 when I look at the pointer value of q and I get An Answer of 15726200 Wehn I look at the pointer value of q + 1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
using namespace std;
int main()
{
        int val = 20;
	double valD = 20;
	int *p = &val;
	double *q;
	q = &valD;
	cout << "Memory Address" << endl;
	cout << p == p + 1;
	cout << endl;
	cout << q == q + 1;
	cout << endl;
	cout << p == q;
	cout << endl;
	cout << q == p;
	cout << endl;
	cout << p == q + 1;
	cout << endl;
	cout << q == p + 1;
	cout << endl;
	cout << "Now Compare Pointer Value" << endl;
	cout << (unsigned long)(p) << endl;
	cout << (unsigned long) (p + 1) << endl;
	cout << (unsigned long)(q) << endl;
	cout << (unsigned long) (q + 1) << endl;
	cout <<"--------" << endl;
        return 0;
}
Last edited on
When you use == I do not think you are doing anything to the pointer.
Because == is just a comparison operator that returns true or false.
And then you are just printing one of the the pointers addresses with cout.

I think you need to use an assignment operator = instead.
Last edited on
I agree
Last edited on
What exactly are you trying to print with cout << p == p + 1;?

By the way the code in the first post shouldn't even compile because there are several errors, this is one of the lines that should cause an error.

The Memory Address
If you want to print the memory address what is the purpose of the calculation? Why not just print the address? cout << p; Remember p is a pointer that is pointing to val, so printing p will print the address to print the value you need to de-reference the pointer:

cout << "The address of val is " << p << " and the value held in that address is: " << *p << endl;


Wow so I was just over thinking it then? Then I can just compare the addresses and values in each pointer then right?
@jlb
Wow so I was just over thinking it then?

Probably.

Then I can just compare the addresses and values in each pointer then right?

Possibly. But I really don't know why you would want to compare the addresses.





Topic archived. No new replies allowed.