943,604 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 688
  • C++ RSS
Aug 26th, 2009
0

how to print the address stored by char *

Expand Post »
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 ....
Similar Threads
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Aug 26th, 2009
1

Re: how to print the address stored by char *

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:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Coding for array pattern matching
Next Thread in C++ Forum Timeline: [Help] Game Bot





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC