|
-
March 11th, 2005, 07:03 PM
#1
just curious
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
-
March 11th, 2005, 07:06 PM
#2
Re: just curious
It's the address of the memory that stores the address of v1.
"Get ready, cuz this ain't funny. My name is Mike D and I'm about to get money."
-
March 11th, 2005, 07:11 PM
#3
Re: just curious
Ok, output 2nd gives the address of the memory that store the v1. But the output 2nd and 3rd are different.
-
March 11th, 2005, 07:17 PM
#4
Re: just curious
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.
Last edited by dro873; March 11th, 2005 at 11:32 PM.
"Get ready, cuz this ain't funny. My name is Mike D and I'm about to get money."
-
March 11th, 2005, 07:32 PM
#5
Re: just curious
-
March 12th, 2005, 03:16 PM
#6
Re: just curious
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.
"Programs must be written for people to read, and only incidentally for machines to execute."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|