1、在C语言中,类型检查不是很严格,因此可以将void*赋值给 type *。
//.c文件中
int a = 0;
int *ptr = &a;
void *ptr1 = ptr;
int *ptr2 = ptr1;//允许将void*类型赋值给int*,不会报错
2、在C++中类型检查比较严格,因此不允许将void*赋值给type *。
//.cpp文件中
int a = 0;
int *ptr = &a;
void *ptr1 = ptr;
int *ptr2 = ptr1;//不允许将void*类型赋值给int*,会报错
int *ptr3 = (int*)ptr1;//需要强制类型转换,才不会报错
对于,返回void*的函数,如果要将返回值赋值给type *,需要进行类型转换。