将结构指针转换为void

时间:2014-02-21 06:45:52

标签: void-pointers

MonkeyStruct *mnkS = (MonkeyStruct*)malloc(sizeof(MonkeyStruct)); void *vS = (void *)mnkS; ManStruct * manS = (ManStruct *)vS;

在上面的代码段中是否有必要首先将mnkS转换为void,然后将其分配给void
   或者我们可以简单地做void *vS=mnkS

1 个答案:

答案 0 :(得分:1)

Void指针可以分配给任何数据类型。

void *ptr;    // ptr is declared as Void pointer
char cn;
int in;
float fn;
ptr = &cn;  // ptr has address of character data
ptr = ∈  // ptr has address of integer data
ptr = &fn;  // ptr has address of float data

因此你可以指定

void *vS=mnkS

没有施法。