[英]Why does streaming a char pointer to cout not print an address?
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. 当我使用printf()
打印一个char指针时,它使用转换说明符来决定是应打印地址还是根据%u或%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? 但是,当我想要做同样的事情cout
,怎么会cout
决定什么应该地址和整个字符串中打印? 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. 在这里,在我的GNU编译器中, cout
试图将ch输出为字符串。
How I can get address of ch
via cptr
using cout
? 如何使用cout
通过cptr
获取ch
地址?
Overload resolution selects the ostream& operator<<(ostream& o, const char *c);
重载分辨率选择ostream& operator<<(ostream& o, const char *c);
which is used for printing C-style strings. 用于打印C风格的字符串。 You want the other ostream& operator<<(ostream& o, const void *p);
您想要另一个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;
cout
prints a string if it receives a char *
, simple as that. 如果cout
收到一个char *
,那么它会打印一个字符串,就这么简单。
Here are the overloads for operator <<
for ostream
: 这是ostream
operator <<
的重载:
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*
. 因此,您需要强制转换为const void*
。
I would just cast it to a void* so it doesn't try to interpret it as a C-string: 我只是将其转换为void *,因此它不会尝试将其解释为C字符串:
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). 但是,一个更安全的选择是使用dirkgently的答案中的static_cast(这样,至少在编译时检查了强制类型转换)。
As Luchian said, cout knows what to print based on the type. 正如Luchian所说,cout知道根据类型打印什么。 If you want to print the pointer value, you should cast the pointer to void* which will be interpated as a pointer. 如果要打印指针值,则应将指针强制转换为void *,它将插入为指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.