3

When I print a char pointer with printf(), it makes the decision with conversion specifier whether the address should be printed or the whole string according to %u or %s.

But when I want to do the same thing with cout, how will cout decide what should be printed among address and whole string? Here is an example source:

int main()
{
  char ch='a';
  char *cptr=&ch;
  cout<<cptr<<endl;
  return 0;
}

Here, in my GNU compiler, cout is trying to output ch as a string.

How I can get address of ch via cptr using cout?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
amin__
  • 1,018
  • 3
  • 15
  • 22
  • 1
    Possible duplicate of [cout << with char\* argument prints string, not pointer value](http://stackoverflow.com/questions/17813423/cout-with-char-argument-prints-string-not-pointer-value) – GingerPlusPlus Jan 05 '16 at 16:43

4 Answers4

16

Overload resolution selects the ostream& operator<<(ostream& o, const char *c); which is used for printing C-style strings. You want the other ostream& operator<<(ostream& o, const void *p); to be selected. You are probably best off with a cast here:

 cout << static_cast<void *>(cptr) << endl;
dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • `static_cast` should be sufficient enough in this case, since we're talking about basic data type pointer. – Zeta Jun 03 '12 at 10:21
7

cout prints a string if it receives a char *, simple as that.

Here are the overloads for operator << for ostream:

ostream& operator<< (bool val);
ostream& operator<< (short val);
ostream& operator<< (unsigned short val);
ostream& operator<< (int val);
ostream& operator<< (unsigned int val);
ostream& operator<< (long val);
ostream& operator<< (unsigned long val);
ostream& operator<< (float val);
ostream& operator<< (double val);
ostream& operator<< (long double val);
ostream& operator<< (const void* val);

ostream& operator<< (streambuf* sb);

ostream& operator<< (ostream& ( *pf )(ostream&));
ostream& operator<< (ios& ( *pf )(ios&));
ostream& operator<< (ios_base& ( *pf )(ios_base&));

ostream& operator<< (ostream& out, char c );
ostream& operator<< (ostream& out, signed char c );
ostream& operator<< (ostream& out, unsigned char c );


//this is called
ostream& operator<< (ostream& out, const char* s );
ostream& operator<< (ostream& out, const signed char* s );
ostream& operator<< (ostream& out, const unsigned char* s );

If you want the address, you want:

ostream& operator<< (const void* val);

so you need to cast to const void*.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
4

I would just cast it to a void* so it doesn't try to interpret it as a C-string:

cout << (void*) cptr << endl;

However, a safer option would be to use static_cast as in dirkgently's answer (that way the cast is at least checked at compile time).

Corbin
  • 33,060
  • 6
  • 68
  • 78
  • 1
    c-style cast ? wouldn't it be safer to use a c++ cast ? – Adrien Plisson Jun 03 '12 at 10:22
  • @AdrienPlisson It's less safe than C++ casts other than reinterpret_cast. In a case as trivial as this, I wouldn't bother with a C++ style cast, though I suppose for completeness, I should mention it. It's hard to foresee a situation in which a cast to void* just to cout it would end up being harmful, though I'm sure once a code base got large enough, some really weird casting could happen. – Corbin Jun 03 '12 at 10:28
  • @Corbin: For `void *`, C++11's `reinterpret_cast` is just okay. See [Issue #1120](http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1120). – dirkgently Jun 03 '12 at 10:34
0

As Luchian said, cout knows what to print based on the type. If you want to print the pointer value, you should cast the pointer to void* which will be interpated as a pointer.

Vincent
  • 1,027
  • 1
  • 11
  • 20