Help with pointers

Ok, so I have this bit of code here. It compiled, but I don't understand part of it. Shouldn't cout<< *foo; spit out a random hexadecimal number, since *foo points to the address of ptr_x?

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>
using namespace std;
int main()
{
	int ptr_x= 25;
	int *foo = &ptr_x;
	cout<< *foo;
}
Last edited on
Shouldn't cout<< *foo; spit out a random hexadecimal number

No. The output should be 25.

Line 7 is a pointer which is initialized to the address of ptr_x.

Line 8: Note the * in front of foo, this dereferences the pointer, meaning to print what foo points to. If line 8 were simply cout << foo; then yes the output would appear to be a random number, but would in fact be the address of ptr_x. BTW, it will print in decimal since that is the default and you haven't changed the mode to hex.
@abstractionanon,
When printing addresses my compiler automatically changes it to hexadecimal unless I specify the address.
Last edited on
loopdydoo,

First 15 minutes of this will clear it all up for you.
http://www.youtube.com/watch?v=Rxvv9krECNw
Topic archived. No new replies allowed.