Joao Miguel Posted April 21, 2013 at 03:14 AM Report #504209 Posted April 21, 2013 at 03:14 AM (edited) Boa noite. Tenho um código cujo objectivo é inserir de forma ordenada, por ano, numa lista ligada. Contudo, quando compilo, dá erro e não estou a conseguir descobrir porquê. Cá vai o código: typedef struct listaEntradas { int ano; struct listaEntradas *seg; } *LISTA_ENTRADAS, NODO_LISTA_ENTRADAS; static LISTA_ENTRADAS *lent = NULL; void inserir_ordenado_lista_entradas(int ano) { LISTA_ENTRADAS *lent_aux = NULL; LISTA_ENTRADAS *pa = NULL; LISTA_ENTRADAS *corr = *lent; int count = 1; lent_aux = (LISTA_ENTRADAS *) malloc(sizeof(NODO_LISTA_ENTRADAS)); lent_aux->ano = ano; lent_aux->seg = NULL; while((corr != NULL) && (count)) { if(ano < corr->ano) count = 0; else { pa = corr; corr = corr->seg; } } lent_aux->seg = corr; if(pa == NULL) *lent = lent_aux; else pa->seg = lent_aux; } Os erros que obtenho são: listaEntradas.c: In function ‘inserir_ordenado_lista_entradas’: listaEntradas.c:15: warning: initialization from incompatible pointer type listaEntradas.c:19: error: request for member ‘ano’ in something not a structure or union listaEntradas.c:20: error: request for member ‘seg’ in something not a structure or union listaEntradas.c:24: error: request for member ‘ano’ in something not a structure or union listaEntradas.c:28: error: request for member ‘seg’ in something not a structure or union listaEntradas.c:31: error: request for member ‘seg’ in something not a structure or union listaEntradas.c:32: warning: assignment from incompatible pointer type listaEntradas.c:33: error: request for member ‘seg’ in something not a structure or union Alguma ideia do que possa estar mal? Obrigado. Edited April 21, 2013 at 03:14 AM by Joao Miguel
HappyHippyHippo Posted April 21, 2013 at 05:23 AM Report #504210 Posted April 21, 2013 at 05:23 AM typedef struct listaEntradas { // ... } *LISTA_ENTRADAS; // <-- é um ponteiro para a estrutura // ... void inserir_ordenado_lista_entradas(int ano) { LISTA_ENTRADAS *lent_aux = NULL; // <--- ponteiro para ponteiro para a estrutura LISTA_ENTRADAS *pa = NULL; // <--- idem, aspas, aspas LISTA_ENTRADAS *corr = *lent; // <--- .... IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
Joao Miguel Posted April 21, 2013 at 10:58 PM Author Report #504256 Posted April 21, 2013 at 10:58 PM (edited) Boa noite. Venho só dizer que consegui resolver o problema e já está a funcionar. Obrigado HappyHippyHippo, com os teus comentários consegui perceber uma coisa sobre apontadores que ainda não tinha percebido e por conseguinte, corrigir a função. Deixo aqui o código corrigido: typedef struct listaEntradas { int ano; struct listaEntradas *seg; } *LISTA_ENTRADAS, NODO_LISTA_ENTRADAS; static LISTA_ENTRADAS lent = NULL; void inserir_ordenado_lista_entradas(int ano) { LISTA_ENTRADAS lent_aux = NULL; LISTA_ENTRADAS pa = NULL; LISTA_ENTRADAS corr = lent; int count = 1; lent_aux = (LISTA_ENTRADAS) malloc(sizeof(NODO_LISTA_ENTRADAS)); lent_aux->ano = ano; lent_aux->seg = NULL; while((corr != NULL) && (count)) { if(ano < corr->ano) count = 0; else { pa = corr; corr = corr->seg; } } lent_aux->seg = corr; if(pa == NULL) lent = lent_aux; else pa->seg = lent_aux; } Edited April 21, 2013 at 10:59 PM by Joao Miguel
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now