C中的空指针

c中的空指针

C 中的 void 指针是不与任何数据类型关联的指针。它指向存储中的某个数据位置,意味着指向变量的地址。它也称为通用指针。在 C 语言中,malloc() 和 calloc() 函数返回 void * 或通用指针。

它有一些限制 –

1) 由于 void 指针的原因,指针运算不可能使用 void 指针具体大小。

2)它不能用作解引用。

算法

Begin   Declare a of the integer datatype.      Initialize a = 7.   Declare b of the float datatype.      Initialize b = 7.6.   Declare a pointer p as void.   Initialize p pointer to a.   Print “Integer variable is”.      Print the value of a using pointer p.   Initialize p pointer to b.   Print “Float variable is”.      Print the value of b using pointer pEnd.

登录后复制

这是一个简单的示例 –

示例代码

 实时演示

#includeint main() {   int a = 7;   float b = 7.6;   void *p;   p = &a;   printf("Integer variable is = %d", *( (int*) p) );   p = &b;   printf("Float variable is = %f", *( (float*) p) );   return 0;}

登录后复制

输出

Integer variable is = 7Float variable is = 7.600000

登录后复制

以上就是C中的空指针的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2582751.html

(0)
上一篇 2025年3月6日 14:23:35
下一篇 2025年3月6日 14:23:42

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论