不完整类型“void”不可分配c

cas*_*235 1 c pointers

我遇到以下指向 void 变量的双指针错误void ** foo。尽管我正在转换结构类型,但我不明白为什么会收到此错误。现在不应该*heap->foo[0]包含/指向 的地址吗personA

typedef struct Person{
    char * name;
    char age;
}Person;

typedef struct myStruct{
    void ** foo;
}myStruct;

void initialize(myStruct * H, int numbOfPersons){
     H->foo = malloc(sizeof(myStruct*));
    *H->foo = malloc(sizeof(myStruct)*numberOfPersons);

 }

void insert(myStruct * H, void * personA){
    *H->foo[0] = (myStruct)personA;  //error here
}

int main(void){
    myStruct heap;
    int numberOfPpl = 2;
    initialize(heap, numberOfPpl);

    Person A;
    A.grade = 10
    strcpy(A->name, "Jason");

    Insert(&heap, &A); 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

incomplete type 'void' is not assignable
                 *H->foo[0] = (Student*)I;
Run Code Online (Sandbox Code Playgroud)

Kla*_*äck 5

类型void不可分配!赋值的左侧必须具有可赋值类型。您需要投射作业的左侧。棘手的部分是您需要在指针级别进行转换:

*(myStruct*)H->foo[0] = *personA;
Run Code Online (Sandbox Code Playgroud)

您的代码中还存在一些其他错误。调高编译器中的警告级别,以便您可以看到它们(-Wall如果您使用 gcc)。