How to convert int* to int

前端 未结 7 835
无人共我
无人共我 2020-12-30 01:29

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?

7条回答
  •  生来不讨喜
    2020-12-30 01:58

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

提交回复
热议问题