As you can see in my code bellow I don't mess with e[i].politico
in the for cycle where my problem occurs, but for some reason, after perfoming a strcpy onto e[i].risco
of a predifined char array, e[i].politico
gets appended with e[i].risco
's content. I can't really find the reason why this happens. Any guidance in debugging is appreciated.
Struct:
typedef struct {
int ent;
char nome[50];
int cc;
int nif;
char naci[50];
char resii[50];
char politico[3];
char risco[50];
} ENT;
Where i scan for e[i].politico
:
scanf(" %d", &aux);
if (aux == 1) {
char s[] = "SIM";
strcpy(e[i].politico, s);
} else {
char n[] = "NAO";
strcpy(e[i].politico, n);
}
printf("1-%s ", e[i].politico);//Value is correct ("SIM")
Where the code fails:
int aux, i;
for (i = 0; i < sizeoficial; i++) {
for (aux = 0; aux < 3; aux++) {
printf("\nBefore get in IF => %s", e[i].politico);//Value is correct ("SIM")
if (strcmp(e[i].naci, riskhigh[aux]) == 0 || strcmp(e[i].resii, riskhigh[aux]) == 0 || strcmp(e[i].politico, "SIM") == 0) {
char r[] = "RISCO ALTO";
printf("\nBefore strcpy => %s", e[i].politico);//Value is correct ("SIM")
strcpy(e[i].risco, r);
printf("\nRisco => %s", e[i].risco);//Value is correct ("RISCO ALTO")
printf("\nAfter strcpy => %s", e[i].politico);//Value is wrong("SIMRISCO ALTO")
break;
} else if ((strcmp(e[i].naci, riskmed[aux]) == 0 || strcmp(e[i].resii, riskmed[aux]) == 0) &&
strcmp(e[i].risco, "RISCO ALTO") != 0) {
char r[] = "RISCO MEDIO";
strcpy(e[i].risco, r);
} else {
char r[] = "RISCO BAIXO";
strcpy(e[i].risco, r);
}
}
}
Output: