如何将int *转换为int [英] How to convert int* to int

查看:610
本文介绍了如何将int *转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出 int 的指针,如何获取实际的 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?

推荐答案

p>使用* on指针获取指向的变量(取消引用)。

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

这篇关于如何将int *转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆