
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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号