string a = "asdf";
cout<<&a[0];
cout<<a[0];
Why are these two outputs different? Why is &a[0]
not the address but the whole string?
为什么这两个输出不同?为什么&a[0]不是地址而是整个字符串?
3 个解决方案
#1
13
&a[0]
has type char *
. Stream operator <<
is deliberately overloaded for const char *
arguments to output zero-terminated string (C-style string) that begins at that address. E.g. if you do
bb&a b0有char类型*。流操作符< <被故意重载,以便const char *参数输出从该地址开始的零终止字符串(c风格的字符串)。例如,如果你< p>
const char *p = "Hello World!";
cout << p;
it is that overloaded version of <<
that makes sure the "Hello World!"
string itself is sent to output, not the pointer value.
是重载的<< < <确保“hello world!”字符串本身被发送到输出,而不是指针值。< p>
And this is exactly what makes your code to output the entire string as well. Since C++11 std::string
objects are required to store their data as zero-terminated strings and &a[0]
is nothing else than a pointer to the beginning of the string stored inside your a
object.
这就是代码输出整个字符串的原因。由于C+ 11 std::string对象被要求将其数据存储为零终止字符串,而[0]只不过是存储在&a对象中的字符串开头的指针。
#2
1
&a[0]
yields type char*
. This is a type for which operator<<()
is overloaded. This particular overload prints the characters starting at the address until it finds a null-character, '\0'
. It won't print the address like you'd expect.
和[0]收益率char *类型。这是一个操作符<<()被重载的类型。这个特殊的重载打印从地址开始的字符,直到找到一个空字符'\0'。它不会像你预期的那样打印地址。
Since you need the address, there's std::addressof()
in the standard library:
由于需要地址,标准库中有std::addressof():
std::cout << std::addressof(a[0]);
you can also cast to void*
which is almost like the above variant:
你也可以施放到void*,这几乎和上面的变体一样:
std::cout << static_cast<void*>(&a[0]);
#3
1
When printing a pointer to a standard library output stream, if it's char*
or const char*
, the null-terminated string pointed to will be printed, rather than the address itself. If you want to have the address printed:
当打印指向标准库输出流的指针时,如果是char*或const char*,则将打印指向的以null结尾的字符串,而不是地址本身。如要印上地址:
cout << static_cast<const void*>(&a[0]);
(Trivia: if the pointer type isn't convertible to const void*
either---because it is a function pointer or a pointer to member or it is volatile---then it is converted to bool
.)
(琐事:如果指针类型也不能转换为const void*——因为它是一个函数指针或成员指针,或者它是易变的——那么它就被转换为bool。)
#1
13
&a[0]
has type char *
. Stream operator <<
is deliberately overloaded for const char *
arguments to output zero-terminated string (C-style string) that begins at that address. E.g. if you do
bb&a b0有char类型*。流操作符< <被故意重载,以便const char *参数输出从该地址开始的零终止字符串(c风格的字符串)。例如,如果你< p>
const char *p = "Hello World!";
cout << p;
it is that overloaded version of <<
that makes sure the "Hello World!"
string itself is sent to output, not the pointer value.
是重载的<< < <确保“hello world!”字符串本身被发送到输出,而不是指针值。< p>
And this is exactly what makes your code to output the entire string as well. Since C++11 std::string
objects are required to store their data as zero-terminated strings and &a[0]
is nothing else than a pointer to the beginning of the string stored inside your a
object.
这就是代码输出整个字符串的原因。由于C+ 11 std::string对象被要求将其数据存储为零终止字符串,而[0]只不过是存储在&a对象中的字符串开头的指针。
#2
1
&a[0]
yields type char*
. This is a type for which operator<<()
is overloaded. This particular overload prints the characters starting at the address until it finds a null-character, '\0'
. It won't print the address like you'd expect.
和[0]收益率char *类型。这是一个操作符<<()被重载的类型。这个特殊的重载打印从地址开始的字符,直到找到一个空字符'\0'。它不会像你预期的那样打印地址。
Since you need the address, there's std::addressof()
in the standard library:
由于需要地址,标准库中有std::addressof():
std::cout << std::addressof(a[0]);
you can also cast to void*
which is almost like the above variant:
你也可以施放到void*,这几乎和上面的变体一样:
std::cout << static_cast<void*>(&a[0]);
#3
1
When printing a pointer to a standard library output stream, if it's char*
or const char*
, the null-terminated string pointed to will be printed, rather than the address itself. If you want to have the address printed:
当打印指向标准库输出流的指针时,如果是char*或const char*,则将打印指向的以null结尾的字符串,而不是地址本身。如要印上地址:
cout << static_cast<const void*>(&a[0]);
(Trivia: if the pointer type isn't convertible to const void*
either---because it is a function pointer or a pointer to member or it is volatile---then it is converted to bool
.)
(琐事:如果指针类型也不能转换为const void*——因为它是一个函数指针或成员指针,或者它是易变的——那么它就被转换为bool。)