Click to See Complete Forum and Search --> : just curious


manojg
March 11th, 2005, 06:03 PM
Hi,

In this code, the output of the 2nd cout is just the address of v1. What is the output of the 3rd cout? Does it mean anything?

main()
{

int *p1, v1;

p1 = &v1;
v1 = 20;
cout << *p1 << endl;
cout << p1 << endl;
cout << &p1 << endl;
return 0;
}

Thanks.
Manoj

dro873
March 11th, 2005, 06:06 PM
It's the address of the memory that stores the address of v1.

manojg
March 11th, 2005, 06:11 PM
Ok, output 2nd gives the address of the memory that store the v1. But the output 2nd and 3rd are different.

dro873
March 11th, 2005, 06:17 PM
Yes, they are different. When you create int v1, it allocates a block of memory that can hold an integer. When you create int *p1, it allocates a block of memory that can hold the address of an integer (the address is a numerical value). When you output p1, it tells you the address of the block of memory that holds the value of v1. When you output &p1, it tells you the address of the block of memory that holds the address of v1.

manojg
March 11th, 2005, 06:32 PM
Thanks dro873.

RoboTact
March 12th, 2005, 02:16 PM
It doesn't matter in fact what does the p1 pointer store: &p1 would be the same. It's just the address of object p1, which is a pointer and can store an address of another object. And no matter what it stores it is located at the same address, so &p1 would be also the same.