954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to print the address stored by char *

cout<<
worked out for every other pointer data type but... when char is fed it show the pointed address.....(which every one knows)

but i want to see the address stored in the char pointer how to see it..

i was thinking using[icode] printf() [icode] but ruined out by not knowing what kind of data type is the used to store address...

I am curious in which data type is used to store mem address....
long, int ....

Rhohitman
Junior Poster in Training
86 posts since Dec 2007
Reputation Points: 10
Solved Threads: 5
 

Most of the time when a programmer prints a char*, he wants a string to be printed, not the address. cout assumes that is what you want and to get around it, you need to cast to a different pointer type. void* is a good choice because an object pointer can be cast to void* safely:

#include <iostream>

int main()
{
    char const* p = "string";

    std::cout << p << '\n'
              << (void*)p << '\n';
}

On a side note, this is the same as how printf works with the %p modifier. To be 100% safe, the pointer should be cast to void* because that is the type %p expects:

#include <cstdio>

int main()
{
    char const* p = "string";

    std::printf("%s\n%p\n", p, (void*)p);
}
Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You