Domanda

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:

#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 *'
È stato utile?

Soluzione

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;
};
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top