取消引用空指针 [英] Dereference void pointer

查看:208
本文介绍了取消引用空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使铸造一个空指针后,我收到编译错误而取消引用它。
任何人都可以请让我知道这样做的原因。

  INT lVNum = 2;
无效* lVptr;
lVptr =为(int *)及lVNum;的printf(\\ nlVptr [60]为%d \\ n,l​​Vptr [1]);


解决方案

的printf(\\ nlVptr [60]为%d \\ n,*(INT *)lVptr);

这将void指针转换为一个指向 INT ,然后取消对它的引用正确。

如果你想要把它当作一个数组(之一),你可以做一个稍微难看的((INT *)lVptr)[0] 。使用 [1] 是出界的,因此不是一个好主意(为 lVptr [60] .. 。)

Even after casting a void pointer, I am getting compilation error while dereferencing it. Could anyone please let me know the reason of this.

int lVNum = 2;
void *lVptr;
lVptr = (int*)&lVNum;

printf("\nlVptr[60 ] is  %d \n",lVptr[1]);

解决方案

printf("\nlVptr[60 ] is %d \n", *(int*)lVptr);

This will cast the void pointer to a pointer to an int and then dereference it correctly.

If you want to treat it as an array (of one), you could do a slightly ugly ((int *)lVptr)[0]. Using [1] is out of bounds, and therefore not a good idea (as for lVptr[60]...)

这篇关于取消引用空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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