how to print the address stored by char *

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

how to print the address stored by char *

 
0
  #1
Aug 26th, 2009
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 ....
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: how to print the address stored by char *

 
1
  #2
Aug 26th, 2009
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:
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. char const* p = "string";
  6.  
  7. std::cout << p << '\n'
  8. << (void*)p << '\n';
  9. }
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:
  1. #include <cstdio>
  2.  
  3. int main()
  4. {
  5. char const* p = "string";
  6.  
  7. std::printf("%s\n%p\n", p, (void*)p);
  8. }
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC