1
With a view to the following structures:
typedef struct celEstado *apontadorEstado;
typedef struct{
char nome[30];
int populacao;
int beneficiarios;
int qtdCidades;
float idh;
apontadorEstado proxEstado;
apontadorCidade Arv;
}celEstado;
I’d like to ask you a question about apontadorEstado
.
When I wrote the structure, I had in mind that I was creating an pointer struct that received the address of a state cell, but now I’m in doubt, because whenever I use a variable of the type apontadorEstado
the compiler issues Warning because, according to him, I’m using a kind of pointer incompatible with the type struct celEstado
. On the other hand, I have to put a pointer to the next state of the list, and I can’t declare the same in the form of celEstado *proxEstado
because the compiler won’t allow it, so the way I found to do that was with the apontadorEstado
Exemplifying the error:
int main(){
celEstado newEstado; //instância da _struct_ celEstado
apontadorEstado aux; //Auxiliar do tipo apontadorEstado(Ponteiro)
strcpy(newEstado.nome, "Bahia"); //inserindo o nome do estado
aux = newEstado.proxEstado; // aux recebendo o endereço do próximo estado
aux = (celEstado*)malloc(sizeof(celEstado));//alocando a memória pra uma célula estado
strcpy(aux->nome, "Amazon");//o erro está aqui
printf("%c", newEstado.nome[0]);//imprimindo o primeiro caractere
printf("%c", aux->nome[0]);
return 0;
}
Printed error:
main. c:25:15: error: Dereferencing Pointer to incomplete type 'pointing strcpy(aux->name, "Amazon");
Warning pointer:
main. c:24:9: Warning: assignment from incompatible Pointer type [-Wincompatible->Pointer-types] aux = (celEstado*)malloc(sizeof(celEstado));
Objectively:
- I’m wrong about the meaning of
apontadorEstado
? - If not, why compiler error?
- How to declare a pointer to the next state correctly?
Sorry for the lack of code, I edited the question with the applied code and with the printed error
– H.Lima
Yes, that’s what I want, because I need to make a chained list of Brazilian states, each with a pointer to a tree of cities that belong to it. There I am breaking my head with the function that will read the files with the information and later store them in the cells.
– H.Lima