首页 > 后端开发 > C++ > 正文

C中的空指针

WBOY
发布: 2023-09-09 13:21:09
转载
766 人浏览过

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 p
End.
登录后复制

这是一个简单的示例 -

示例代码

 实时演示

#include<stdlib.h>
int main() {
   int a = 7;
   float b = 7.6;
   void *p;
   p = &a;
   printf("Integer variable is = %d", *( (int*) p) );
   p = &b;
   printf("\nFloat variable is = %f", *( (float*) p) );
   return 0;
}
登录后复制

输出

Integer variable is = 7
Float variable is = 7.600000
登录后复制

以上是C中的空指针的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!