0

Hi, I've tried to output pointer in decimal but can't do it

int a = 10;
	int* p = &a;
	cout.unsetf(ios_base::hex);
	cout.unsetf(ios_base::oct);
	cout.unsetf(ios_base::dec);
	cout << showbase << dec << p << endl;

it still shows this output in hexadecimal format. Any reason why this doesn't work. Thanks in advance.

2
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by atch
0

Can you summarise the effect in a complete program we can try ourselves, and not just a context-free program fragment that could do anything.

0

Well, that's what I have in main and I would like to have output from pointer to be in decimal format instead of hexadecimal.

0

Are you expecting 10 to be printed by that code?

Because that would be cout << showbase << dec << [B]*[/B]p << endl; To actually output a pointer in another base, you'll probably have to cast it to something which isn't a pointer (in other words, ugly)

Maybe cout << showbase << dec << [B](unsigned long)[/B]p << endl; I'm guessing the output of a pointer representation ignores base, because the idea itself is meaningless.

0

Yes, Salem that is what I was asking for, that is do I have to explicitly cast pointer to another type to print it as "another type". Thank you for your help.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.