为什么我要获得内存地址而不是实际值?指针C++
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是您需要的。
当您尝试将某些内容输出到Stdout时,编译器会自动推断其类型并调用相应的功能。在您的情况下,p是指针类型,因此打印地址,并且由于 *p是int类型,因此如果要打印值,则应使用 *p。
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.
我认为您对提出指针的误解。
,这意味着要检索指向指向的值。它不会以任何方式改变指针本身。
p
仍然是*p = y;
之后的指针。所有
*p = y
确实是要更改值p
指向,它不会更改指针本身。I think you have a misconception about dereferencing pointers.
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 valuep
is pointing to, it doesn't change the pointer itself.