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>
usingnamespace std;
int main()
{
int ptr_x= 25;
int *foo = &ptr_x;
cout<< *foo;
}
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.