nram Posted May 18, 2009 at 08:31 PM Report #265174 Posted May 18, 2009 at 08:31 PM Boas malta, ando aqui com mais um problemazito numa funçao... O que pretendo é criar espaço com o malloc numa struct que chama outra struct... para isso usei as seguintes structs: typedef struct condRules { struct listRules *lrules; //Vai buscar a lista de regras struct typeRules *tpregras; //Buscar onde se aplica à outra estructura char changeF; //Trocar Valor -> changeF Values struct condRules *nextCond, *prevCond; //ProxCond -> Next Condicion } CRULES, *CCRULES; CCRULES head=NULL, tail=NULL; typedef struct listRules { char neighbor; //Vizinhança -> neighborhood char op; //Operador -> operator || 1 a 6 char val[2]; //Valor -> Value struct listRules *nextRule, *prevRule; } LRULES; typedef struct typeRules { char specases; //Casos Especiais -> Special Cases struct typeRules *nexttp, *prevtp; //Proximo tipo -> Next type } TRULES; e a funçao é a seguinte: int addTRULES(char s){ CRULES *a; TRULES *b; //Ultima pos do CRULES: -> (*tail) a=head; //Pesquisar na TRULES b = (TRULES *)malloc(sizeof(TRULES)); if (b == NULL) { printf("Falta de memoria."); exit(1); } b->specases = s; b->nexttp=NULL; while (a!=NULL) { a = a->nextCond; }; if(a->tpregras==NULL) {<--- Porque é que aqui dá erro? b->nexttp=NULL; b->prevtp=NULL; a->tpregras = b; <--- Porque é que aqui dá erro? } while (a->tpregras!=NULL){ a->tpregras=a->tpregras->nexttp; } b->nexttp = a->tpregras->nexttp; a->tpregras->nexttp=b; if (b->nexttp != NULL){ b->prevtp=b->nexttp->prevtp; b->nexttp->prevtp=b; } a->tpregras = b; return 0; } Está sinalizado onde tenho o erro... Cumprimentos
Baderous Posted May 18, 2009 at 08:45 PM Report #265182 Posted May 18, 2009 at 08:45 PM Porque nesse momento o 'a' é NULL (o ciclo while anterior deixa-o a NULL) e, ao tentares aceder a uma posição de algo que está referenciado como NULL, dá erro.
nram Posted May 18, 2009 at 08:46 PM Author Report #265183 Posted May 18, 2009 at 08:46 PM Porque nesse momento o 'a' é NULL (o ciclo while anterior deixa-o a NULL) e, ao tentares aceder a uma posição de algo que está referenciado como NULL, dá erro. é isso mesmo.. thanks. if (a!=NULL) { while (a->nextCond!=NULL){ a = a->nextCond; } };
nram Posted May 18, 2009 at 09:19 PM Author Report #265197 Posted May 18, 2009 at 09:19 PM Se eu fizer um search ele nunca encontra a ultima posiçao... Escreve sempre a mesma posiçao... :S Ou seja, nao encontra NULL, que tenho mal?? int addTRULES(char s){ CRULES *a; TRULES *b; //Ultima pos do CRULES: -> (*tail) a=head; //Pesquisar na TRULES b = (TRULES *)malloc(sizeof(TRULES)); if (b == NULL) { printf("Falta de memoria."); exit(1); } b->specases = s; b->nexttp=NULL; if (a!=NULL) { while (a->nextCond!=NULL){ a = a->nextCond; } if (a->tpregras!=NULL){ while(a->tpregras->nexttp!=NULL){ a->tpregras=a->tpregras->nexttp; } } } if(a->tpregras==NULL) { b->nexttp=NULL; b->prevtp=NULL; a->tpregras = b; } while (a->tpregras->nexttp!=NULL){ a->tpregras=a->tpregras->nexttp; } b->nexttp = a->tpregras->nexttp; a->tpregras->nexttp=b; if (b->nexttp != NULL){ b->prevtp=b->nexttp->prevtp; b->nexttp->prevtp=b; } a->tpregras = b; return 0; } Pesquisa: int pesqSEARCH(void){ CRULES *a; TRULES *b; LRULES *c; a=head; while (a!=NULL) { b=a->tpregras; while (b!=NULL){ printf("S=%c,",b->specases); b=b->nexttp; } c=a->lrules; while (c!=NULL){ printf("%c",c->neighbor); printf("%c",c->op); printf("%s",c->val); c=c->nextRule; } printf("#%c \n",(a->changeF)); a = a->nextCond; } return 0; }
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