tags:

views:

310

answers:

6

Hi guys,

Question is how do i convert a pointer to int to an actual int

I dont know if this is possible or not, but can someone please advise me

Regards

Paul

+3  A: 

use the dereference operator * e.g

void do_something(int *j) {
    int k = *j; //assign the value j is pointing to , to k
    ...
}
nos
This does not answer the question.
Danvil
The int * j has been converted to int k. That seems to answer the question.
CiscoIPPhone
+21  A: 

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 );
Klaim
Safer example than jimka's
Toji
Not sure if static_cast is right for the last sentence, changed to reinterpret_cast ...
Klaim
Is `int` a good type to cast a pointer to? Wouldn't `#include <cstdint>` and `uintptr_t` be more portable?
ndim
+1  A: 

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
sharptooth
This does not answer the question.
Danvil
Why the d/v on this response?
John Dibling
Yeap, I don't get that either.
sharptooth
@Danvil: How so?
GMan
*Converting* a pointer is not the same as dereferencing.
Danvil
+4  A: 

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

phimuemue
+1 for mentioning cast to int
Vlad
This is C++, you should avoid C-style casts.
Seth Johnson
+1  A: 

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.

SF.
+5  A: 

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);
John Dibling
+1 for explicitly mentioning no conversion is happening.
Bryan Oakley