Pointer array in which each element is a row

Asked

Viewed 138 times

1

Hello. I wanted to create a pointer array in which each element pointed to a supposed queue, but when I try to access the (pointer vector) -> (TAD queue) -> (vector inside the queue TAD), I have problems, it does not access.

Following example

typedef struct colegio { // vetor com 180 ponteiros
    void *vetor_filas[180];
} Colegio;

typedef struct registro_fila { // fila com um vetor de 20 espaços
    int qtd_pessoas[20];
} Fila;

Fila* criar_salas() { // criação do TAD fila, retorno ponteiro tipo Fila*
    Fila *nova_fila;
    nova_fila = (Fila*)malloc(sizeof(Fila));

    return nova;
}

void alocarColegiosSalas(Colegio *colegio) { // aloca as filas nos 180 vetores
    int i = 0;

    while (i < 180) {
        colegio->vetor_filas[i] = criar_salas();
        i++;
    }
}

int main() {


    Colegio *novosColegios;

    alocarColegiosSalas(novosColegios);

    int a = 1;
    novoColegio->vetor_filas[0]->qtd_pessoas[0] = a;
 // aqui ele pede um casting do tipo (Fila*) (warning), e avisa que não é uma estrutura nem união.

    printf("%d\n", novoColegio->vetor_filas[0]->qtd_pessoas[0]); 
// avisa que a estrutura é algo que não é um estrutura nem união.


}

I believe I’m doing something wrong in the structures, or something like.

  • He doesn’t do anything because novoColegio->vetor_filas[0] has kind void *. novoColegio->vetor_filas[0] you know nothing of what qtd_pessoas this is. To improve the code, declare Fila before Colegio and declare, within Colegio, Filas* filas[180]; instead of void *vetor_filas[180];.

  • Just reinforcing what @Marcelouchimura has already said, pointers like void* cannot be used without first being converted to another type, as the compiler does not know what type the value is. Hence this instruction colegio->vetor_filas[i] = criar_salas(); won’t work.

  • Hello @Marcelouchimura, I did what you said but without using a pointer to the structure, simply queue Queues[180], and in this way I was able to enter my data, but I came up with another question, in which case, in which I had nested structures, I would need, in a "step" from one structure to another, use the pointer? Abs.

  • To use operator ->, what’s left of it should be a pointer, and what’s right, a field of struct.

No answers

Browser other questions tagged

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