卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章18585本站已运行341

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 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程序用于判断给定的数字是否为强数
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏