简体   繁体   English

如何将 int* 转换为 int

[英]How to convert int* to int

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?我不知道这是否可能,但有人可以建议我吗?

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 );

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 really need to convert the pointer to an int, then you need to cast.但是如果你真的需要将指针转换为 int,那么你需要进行强制转换。 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* p = get_int_ptr();
int val = reinterpret_cast<int>(p);

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.希望这可以帮助。

use the dereference operator * eg使用解引用运算符*例如

void do_something(int *j) {
    int k = *j; //assign the value j is pointing to , to k
    ...
}

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 really know 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*) ,但您很可能会收到编译器警告。

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
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM