当在C中指定类型为void *时,错误不兼容的类型。

时间:2021-05-03 16:13:30

So this is my header file:

这是我的头文件

#define VECTOR_INITIAL_CAPACITY 20

struct _Variable {
    char *variableName;
    char *arrayOfElements;
    int32_t address;
};
typedef struct _Variable Variable;

struct _VariableVector {
    int size; // elements full in array
    int capacity; // total available elements
    Variable variables[VECTOR_INITIAL_CAPACITY];
};
typedef struct _VariableVector VariableVector;

void init(VariableVector *variableVector);

void append(Variable *variable);

Variable* find(char *variableName);

and this is my .c file where I am trying to implement the init method:

这是我的。c文件,我正在尝试实现init方法:

#include "VariableVector.h"

void init(VariableVector *variableVector) {
    variableVector->size = 0;
    variableVector->capacity = VECTOR_INITIAL_CAPACITY;

    // allocate memory for variableVector
    variableVector->variables = malloc(
            sizeof(Variable) * variableVector->capacity); // <== ERROR HERE
}

The error I am getting above is

我得到的误差是

incompatible types when assigning to type 'struct Variable[20]' from type 'void *'

1 个解决方案

#1


0  

You're trying to assign to an array. That's not going to happen. The array is where it is, at the size that it had initially. It can't take a new address and size.

你要给数组赋值。这是不可能的。数组在它的位置,它最初的大小。它不能占用一个新的地址和大小。

variables needs to be a pointer.

变量必须是指针。

struct _VariableVector {
    int size; // elements full in array
    int capacity; // total available elements
    Variable *variables;
};

#1


0  

You're trying to assign to an array. That's not going to happen. The array is where it is, at the size that it had initially. It can't take a new address and size.

你要给数组赋值。这是不可能的。数组在它的位置,它最初的大小。它不能占用一个新的地址和大小。

variables needs to be a pointer.

变量必须是指针。

struct _VariableVector {
    int size; // elements full in array
    int capacity; // total available elements
    Variable *variables;
};