Void指针如何在c中使用? [英] How is the Void pointer can be used in c?

查看:66
本文介绍了Void指针如何在c中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi

这段代码是什么问题:


int main()
{
无效* p;
int * q,x;
x = 10;
q =& x;
* p = * q;
printf(%d",* p);
getch();
返回0;
}

hi

what is the problem in this code:


int main()
{
void *p;
int *q,x;
x=10;
q=&x;
*p=*q;
printf("%d",*p);
getch();
return 0;
}

推荐答案

p 是空指针:没有大小.当您尝试打印无尺寸对象的值时,您期望发生什么?
p is a void pointer: it has no size. When you try to print the value of a sizeless object what do you expect to happen?


无论何时使用void *,通常都希望将其强制转换为所需的对象在将其用于任何用途之前.您不想将其用作void.
Whenever you''re using a void*, you typically would want to cast it to whatever you need it to be before using it for anything. You don''t want to use it as void.


这篇关于Void指针如何在c中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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