Jump to content

Recommended Posts

Posted

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

Posted

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;
	}
};
Posted

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;
}

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.