cout << with char* argument prints string, n

2018-12-31 06:08发布

This:

const char * terry = "hello";
cout<<terry;

prints hello instead of the memory address of the 'h'. Why is this happening?

标签: c++
5条回答
余生请多指教
2楼-- · 2018-12-31 07:02

The << operator on std::cout is overloaded. Its behavior depends on the type of the right operand. (It's actually several different functions, all named operator<<; the compiler decides which one to call.)

If you give it a char* or const char*, it treats the operand as a pointer to (the first character of) a C-style string, and prints the contents of that string:

const char * terry = "hello";
cout << terry; // prints "hello"

If you give it a char value, it prints that value as a character:

cout << *terry;   // prints "h"
cout << terry[0]; // the same

If you give it a pointer of type void*, it prints that pointer value (in some implementation-defined way, typically hexadecimal):

cout << static_cast<const void*>(terry); // prints something like 0x4008e4

Treating a char* or const char* as a pointer to a C-style string is a special case, and the only one (that I can think of) that causes operator<< to print something other than the value of the operand. The reason for this goes back to C++'s roots in C, which doesn't have a "string" type and manipulates strings via char* pointers.

There are numerous other overloads for operator<<, for various integer and floating-point numeric types, for std::string, and so forth.

查看更多
查无此人
3楼-- · 2018-12-31 07:02

"hello" is a string, i.e. the char array. const char* is a pointer to this array, so when you dereference this pointer, you get the value of the first element.

It is like if you have

int a[] = {1, 2, 3};
int *b = a;
cout << *b << endl;

you get just 1 printed.

查看更多
后来的你喜欢了谁
4楼-- · 2018-12-31 07:05

The reason for that is that std::cout will treat a char * as a pointer to (the first character of) a C-style string and print it as such. If you want the address instead, you can just cast it to a pointer that isn't treated that way, something like:

cout << (void *) terry;

(or use the const void * cast if you're worried about casting away constness, something that's not an issue in this particular case).


If you're more of a purist than pragmatist, you can also use the C++ static_cast, along the lines of:

cout << static_cast <const void *> (terry);

though it's unnecessary in this particular case, the cast to a void * will work fine. The following sample code shows all these options in action:

#include <iostream>
int main (void) {
    const char *terry = "hello";
    std::cout << terry << '\n';
    std::cout << (void *) terry << '\n';
    std::cout << (const void *) terry << '\n';
    std::cout << static_cast<const void *> (terry) << '\n';
    return 0;
}

outputting (the address may be different in your environment):

hello
0x8048870
0x8048870
0x8048870

Note that, when using the static_cast, you should ensure you don't try to cast away the constness with static_cast <void *> (that's what const_cast is for). This is one of the checks done by the newer C++ casts and the old-style cast does not have this limitation.

查看更多
公子世无双
5楼-- · 2018-12-31 07:09

cout is overloaded so that when you give it a char*, it will print as a pointer to a C style string. So, it prints out the characters until it hits a null terminating character.

If you used printf instead of cout, you would see the address. You could also cast the pointer to another type, say (void*) and you would also get the address.

查看更多
残风、尘缘若梦
6楼-- · 2018-12-31 07:10

You should change your code to this:

cout << static_cast<const void*>(terry);

The problem is that << operator is overloaded for pointers to C-style strings for printing the content of the string. If you cast it to the raw pointer instead, you will have the default behaviour of printing pointer using iostreams as you want.

查看更多
登录 后发表回答