23

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?

2
  • 4
    You said you’re not sure if that’s possible. Consider: If that were not possible, what would be the use of a pointer? Apr 23, 2010 at 16:36
  • 2
    ill admit it was a daft question
    – paultop6
    Apr 25, 2010 at 9:03

7 Answers 7

49

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 );
2
  • 1
    Is int a good type to cast a pointer to? Wouldn't #include <cstdint> and uintptr_t be more portable?
    – ndim
    Apr 23, 2010 at 15:07
  • (char*)var not needing the leading * confused me so much; I had to use *(int*)var to cast my value to int when it was cast to char just fine without it.
    – UpTide
    Aug 21 at 23:22
11

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

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.

3
  • This is C++, you should avoid C-style casts. Apr 23, 2010 at 15:09
  • 2
    Why? C style cast are part of the language. Why exactly should one avoid them?
    – rxantos
    Feb 4, 2016 at 3:13
  • Mainly, because they are part of the language only for compatibility purposes. Just the fact that you can find erroneous casts via ctrl+f and searching for cast justifies the use of C++-style casting.
    – L0ren2
    Feb 10, 2022 at 11:34
4

use the dereference operator * e.g

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

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.

1

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
1
  • Why the d/v on this response? Apr 23, 2010 at 15:06
-1
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.