Char pointer doesn't return address...

Hi there!

Smaller text this time.

Basically tried to get a pointer of char array to return the address of the elements as well as the values of said elements.
However even though the value is returned fine the address is... odd.

Here is code and output:

CODE:
pcpointer = &firstChar[0];

cout << "The pointer pnpointer is looking at " << pcpointer << " with the value " << *pcpointer << endl;

pcpointer++;

cout << "Increment the pointer then we would look at address " << pcpointer << " with the value of " << *pcpointer << endl;


OUTPUT:
Please now enter a value for the first char: g
Please now enter a value for the second char: f
The pointer pnpointer is looking at gf6 " with the value g
Increment the pointer then we would look at address f7 " with the value of f


See what I mean? Instead of producing a hex memory address it produces this "mish mash" of values and what looks like random numbers.

Is this right? Am I just being dumb? It's okay I can take the truth!

And again thank you looking into this for me.

P.S. As soon as I change the pointer and array type to int the addresses that are returned are fine.
Last edited on
You don't tell us what type pcpointer is.

Assuming pcpointer is a char*, you can do this:

 
cout << "The pointer pnpointer is looking at " << static_cast<void*>(pcpointer) << " with the value " << *pcpointer << endl;


Right now you are suffering from the effects of operator<< overloading. Operator<< expects a char* to point to a NUL-terminated string.
Hi
Sorry PanGalactic, yes pcpointer is a char*.

Thank you for your assistance - that works beautifully now.

Ah, of course null terminated! which in array you WON'T get til the very end... is that right?
I'm sorry if I'm a little slow.

Will now have to read up about static_cast - many thanks again!

T.T.
you can also do this
while printing address use &pcpointer instead pcpointer
Last edited on
@bugme
Unfortunately that only shows the address of the pointer itself, not what the pointer's pointing at.
I think he meant:

&(*pcpointer)
Topic archived. No new replies allowed.