error: expected Expression before struct

Asked

Viewed 59 times

0

Why are you making a mistake? Why?

Error = |21|error: expected Expression before 'Simple list'|

#define MAX 25

typedef struct Rota Rota;
typedef struct ListaRota ListaRota;
typedef struct ListaSimples ListaSimples;
typedef struct ListaDupla ListaDupla;

struct ListaSimples{
    ListaRota *rotas;
    ListaDupla *dados;
    int tamanho;
};

struct ListaRota{
    ListaRota *proximo;
    char *dado[MAX];
};

struct ListaDupla{
    Rota *info;
    int tam[ListaSimples->tamanho];
};

struct Rota{
    Rota *anterior;
    Rota *sucessor;
    char *nomecidade[MAX];
    char *descricao[MAX*2];
};

2 answers

0

The Error

Look at that part:

int tam[ListaSimples->tamanho];

What is the value of ListaSimples->tamanho? Apparently this is your mistake. ListaSimples->tamanho is not a value because ListaSimples, in your code, it is a type of data you created and so this expression is invalid.

Solution

I don’t know exactly what your goal is, but apparently the size of int tam[ListaSimples->tamanho]; is undefined, ie may vary for each run. If you do not know the size then it should be used malloc. Example:

/* Declare como um ponteiro */
int *tam;

/* Faça a alocação quando de fato for criar o objeto */
int *tam = malloc(/* Aqui vai o tamanho */);

OBS: Don’t forget to give free for each pointer you used the malloc.

  • got it, forgot that the vector size has q be initialized

0

struct ListaDupla{
    Rota *info;
    int tam[ListaSimples->tamanho];
};

The error is in int tam[Simple list->size]; in structs or put a certain value or use a pointer unless you use something more complex in certain C revisions from to int tam[] and then set later here is an example

extern const int n;
typedef struct
{
    int len;
    char p[];
} str;
void foo(void)
{
    size_t str_size = sizeof(str);  // equivalent to offsetoff(str, p)
    str *s = malloc(str_size + (sizeof(char) * n));
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.