cpp / c++ 获取指针值或去指针化指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14420257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:19:31  来源:igfitidea点击:

cpp / c++ get pointer value or depointerize pointer

c++pointers

提问by Rasmus

I was wondering if it's possible to make a pointer not a pointer..

我想知道是否有可能使指针不是指针..

The problem is I have a function that accepts a pointer for an paramater for me to easily get a value to that pointer. It's a simple int so I was wondering if I could just get that value without needing to send around a pointer wherever I want the value to land.

问题是我有一个函数,它接受一个参数的指针,以便我轻松获取该指针的值。这是一个简单的 int,所以我想知道是否可以直接获取该值,而无需将指针发送到我想要该值所在的任何位置。

I don't want the function to return the value as an int as it's giving a value to 2 pointers!

我不希望该函数将值作为 int 返回,因为它为 2 个指针提供了一个值!

回答by James McDonnell

To get the value of a pointer, just de-reference the pointer.

要获取指针的值,只需取消对指针的引用。

int *ptr;
int value;
*ptr = 9;

value = *ptr;

value is now 9.

值现在是 9。

I suggest you read more about pointers, this is their base functionality.

我建议你阅读更多关于指针的内容,这是它们的基本功能。