How to convert int* to int

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

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

Tags:

C++

Pointers