C++ 如何将 int* 转换为 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2699534/
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
How to convert int* to int
提问by paultop6
Given a pointer to int
, how can I obtain the actual int
?
给定一个指向 的指针int
,我怎样才能获得实际的int
?
I don't know if this is possible or not, but can someone please advise me?
我不知道这是否可能,但有人可以建议我吗?
回答by Klaim
Use the * on pointers to get the variable pointed (dereferencing).
在指针上使用 * 来获取指向的变量(取消引用)。
int val = 42;
int* pVal = &val;
int k = *pVal; // k == 42
If your pointer points to an array, then dereferencing will give you the first element of the array.
如果您的指针指向一个数组,则取消引用将为您提供该数组的第一个元素。
If you want the "value" of the pointer, that is the actual memory address the pointer contains, then cast it (but it's generally not a good idea) :
如果您想要指针的“值”,即指针包含的实际内存地址,则对其进行强制转换(但这通常不是一个好主意):
int pValValue = reinterpret_cast<int>( pVal );
回答by John Dibling
If you need to get the value pointed-to by the pointer, then that's not conversion. You simply dereference the pointer and pull out the data:
如果您需要获取指针指向的值,那么这不是转换。您只需取消引用指针并提取数据:
int* p = get_int_ptr();
int val = *p;
But if you reallyneed to convert the pointer to an int, then you need to cast. If you think this is what you want, think again. It's probably not. If you wrote code that requires this construct, then you need to think about a redesign, because this is patently unsafe. Nevertheless:
但是如果你真的需要将指针转换为 int,那么你需要进行强制转换。如果您认为这就是您想要的,请再想一想。可能不是。如果您编写的代码需要这种结构,那么您需要考虑重新设计,因为这显然是不安全的。尽管如此:
int* p = get_int_ptr();
int val = reinterpret_cast<int>(p);
回答by phimuemue
I'm not 100% sure if I understand what you want:
我不是 100% 确定我是否理解你想要的:
int a=5; // a holds 5
int* ptr_a = &a; // pointing to variable a (that is holding 5)
int b = *ptr_a; // means: declare an int b and set b's
// value to the value that is held by the cell ptr_a points to
int ptr_v = (int)ptr_a; // means: take the contents of ptr_a (i.e. an adress) and
// interpret it as an integer
Hope this helps.
希望这可以帮助。
回答by nos
use the dereference operator *
e.g
使用解引用运算符,*
例如
void do_something(int *j) {
int k = *j; //assign the value j is pointing to , to k
...
}
回答by SF.
You should differentiate strictly what you want: cast or dereference?
您应该严格区分您想要什么:强制转换还是取消引用?
int x = 5;
int* p = &x; // pointer points to a location.
int a = *p; // dereference, a == 5
int b = (int)p; //cast, b == ...some big number, which is the memory location where x is stored.
You can still assign int directly to a pointer, just don't dereference it unless you reallyknow what you're doing.
您仍然可以将 int 直接分配给指针,除非您真的知道自己在做什么,否则不要取消引用它。
int* p = (int*) 5;
int a = *p; // crash/segfault, you are not authorized to read that mem location.
int b = (int)p; // now b==5
You can do without the explicit casts (int)
, (int*)
, but you will most likely get compiler warnings.
您可以不使用显式强制转换(int)
, (int*)
,但您很可能会收到编译器警告。
回答by sharptooth
Use * to dereference the pointer:
使用 * 取消引用指针:
int* pointer = ...//initialize the pointer with a valid address
int value = *pointer; //either read the value at that address
*pointer = value;//or write the new value
回答by Shi Ming
int Array[10];
int *ptr6 = &Array[6];
int *ptr0 = &Array[0];
uintptr_t int_adress_6 = reinterpret_cast<uintptr_t> (ptr6);
uintptr_t int_adress_0 = reinterpret_cast<uintptr_t> (ptr0);
cout << "difference of casted addrs = " << int_adress_6 - int_adress_0 << endl; //24 bits
cout << "difference in integer = " << ptr6 - ptr0 << endl; //6