
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("
F
.........................................................