是” const wchar_t*"指针?

发布于 2025-02-12 13:21:51 字数 522 浏览 1 评论 0原文

通常,当我们编写时:

int a = 10;
int* ptr = &a;
std::cout << *ptr;

代码输出是:

> 10

但是当我编写这篇文章时:

const wchar_t* str = L"This is a simple text!";
std::wcout << str << std::endl;
std::wcout << &str << std::endl;

代码输出为:

> This is a simple text!
> 012FFC0C

这使我感到困惑。

  • 那个星号符号不是指针吗?
  • 如果是指针,我们如何分配一个值 比地址值?
  • 最高输出不应该在底部和 顶部的底部输出?

Normally when we write:

int a = 10;
int* ptr = &a;
std::cout << *ptr;

Code output is:

> 10

But when I write this:

const wchar_t* str = L"This is a simple text!";
std::wcout << str << std::endl;
std::wcout << &str << std::endl;

Code output is:

> This is a simple text!
> 012FFC0C

So this makes me confused.

  • Doesn't that Asterisk symbol stand for pointer?
  • If it is a pointer, how is it possible for us to assign a value other
    than the address value?
  • Shouldn't the top output be at the bottom and
    the bottom output at the top?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒冷纷飞旳雪 2025-02-19 13:21:51

l“这是一个简单的文本!”是类型const wchar_t [23]包含所有字符串字符以及终止0 char的数组。数组可能会衰减,在这种情况下,它们变成了

const wchar_t* str = L"This is a simple text!";

std :: Wout中发生的情况,可以与&lt;&lt;&lt;一起使用。运算符将如此无效的终止字符作为第二操作数;该操作员打印字符串。

第二个打印只是打印指针的地址,因为&lt;&lt;&lt;运算符处理类型const wchar_t **与任意指针有不同的特点的运算符,因此没有超载的过载。类型(除了第一个打印中所示的某些特定指针类型外)。

L"This is a simple text!" is an array of type const wchar_t[23] containing all the string characters plus a terminating 0 char. Arrays can decay in which case they turn into a pointer to the first element in the array which is what happens in

const wchar_t* str = L"This is a simple text!";

std::wout can be used with a << operator that takes such a null terminated character as second operand; this operator prints the string.

The second print simply prints the address of the pointer, since there is no overload for the << operator handling an argument of type const wchar_t** differently to arbitrary pointer types (except for some specific pointer types as seen in the first print).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文