简体   繁体   中英

How to convert int* to int

Given a pointer to int , how can I obtain the actual 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. 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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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