为什么我要获得内存地址而不是实际值?指针C++

发布于 2025-02-11 04:40:42 字数 167 浏览 3 评论 0原文

int x = 2;
int y=8;
int* p = &x; 
*p=y;

cout << p <<endl;

我的问题是:当我打印p而不是实际值时,为什么要获得内存adress

int x = 2;
int y=8;
int* p = &x; 
*p=y;

cout << p <<endl;

my question is: why do I get the memory adress when I Print p and not the actual value since I already dereferenced it in line 4

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

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

发布评论

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

评论(2

む无字情书 2025-02-18 04:40:42
cout << *p << endl;

是您需要的。
当您尝试将某些内容输出到Stdout时,编译器会自动推断其类型并调用相应的功能。在您的情况下,p是指针类型,因此打印地址,并且由于 *p是int类型,因此如果要打印值,则应使用 *p。

cout << *p << endl;

is what you need.
When you try to output something to stdout, compiler automatically infer its type and call the corresponding function. In your case, p is pointer type, therefore the address is printed, and since *p is int type, you should use *p if you want to print the value.

余厌 2025-02-18 04:40:42

我认为您对提出指针的误解。

...由于我已经在第4行中删除了它

,这意味着要检索指向指向的值。它不会以任何方式改变指针本身。 p仍然是*p = y;之后的指针。

所有*p = y确实是要更改值p指向,它不会更改指针本身。

I think you have a misconception about dereferencing pointers.

...since I already dereferenced it in line 4

Dereferencing means to retrieve the value the pointer is pointing to. It doesn't change the pointer itself in any way. p is still a pointer after *p=y;.

All *p=y does, is to change the value p is pointing to, it doesn't change the pointer itself.

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