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 kindvoid *
.novoColegio->vetor_filas[0]
you know nothing of whatqtd_pessoas
this is. To improve the code, declareFila
beforeColegio
and declare, withinColegio
,Filas* filas[180];
instead ofvoid *vetor_filas[180];
.– Marcelo Shiniti Uchimura
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 instructioncolegio->vetor_filas[i] = criar_salas();
won’t work.– Isac
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.
– Carter Mario
To use operator
->
, what’s left of it should be a pointer, and what’s right, a field of struct.– Marcelo Shiniti Uchimura