ตัวชี้เป็นโมฆะใน C เป็นตัวชี้ที่ไม่เกี่ยวข้องกับชนิดข้อมูลใดๆ มันชี้ไปยังตำแหน่งข้อมูลบางส่วนในการจัดเก็บหมายถึงชี้ไปยังที่อยู่ของตัวแปร เรียกอีกอย่างว่าตัวชี้วัตถุประสงค์ทั่วไป ใน C ฟังก์ชัน malloc() และ calloc() จะคืนค่า void * หรือพอยน์เตอร์ทั่วไป
มันมีข้อจำกัดบางอย่าง -
1) เลขคณิตของตัวชี้เป็นไปไม่ได้กับตัวชี้เป็นโมฆะเนื่องจากมีขนาดที่เป็นรูปธรรม
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