Error: conflicting types for 'firstPerson'

1

I made a function where I pass as argument two integers and two pointers of two struct that I created in order to write the return in a txt file. When running the program, if I give printf() to the return of the function in main it gives the desired value, but if fprintf() no longer gives this value. How can I resolve?

char* primeiraPessoa(int countV, int countF, Visitante *visitante, Funcionario *funcionario){
int hora=0;
char *id;
id = "N/A";
for(int i=0;i<countV;i++){

    if(i==0){

        sprintf(id, "%d", visitante[i].id);
        hora=visitante[i].horaE;

    }else if(hora>visitante[i].horaE){
        sprintf(id, "%d", visitante[i].id);
        hora=visitante[i].horaE;

    }else if(hora==visitante[i].horaE){
        strcat(id,",");
        char idTemp[5];
        sprintf(idTemp, "%d", visitante[i].id);
        strcat(id,idTemp);
    }
}

for(int i=0;i<countF;i++){

    if(hora>funcionario[i].horaE){

        sprintf(id, "%d", funcionario[i].id);
        hora=visitante[i].horaE;

    }else if(hora==funcionario[i].horaE){

        strcat(id,",");
        char idTemp[5];
        sprintf(idTemp, "%d", funcionario[i].id);
        strcat(id,idTemp);

    }
}

return id;
}

To write to the txt file I have the function:

void escreveFicheiro(Visitante *visitante,Funcionario *funcionario,int countV,int countF,int countInvalidas,int countBarradas){
char nome [] = "estatisticas.txt";
FILE * fp = fopen(nome, "w");

if (fp) {
    fprintf(fp,"15 - %s\n",primeiraPessoa(countV,countF,visitante,funcionario));
}
    fclose(fp);

}
    
asked by anonymous 21.06.2018 / 21:10

1 answer

0

This strcat(id,","); will give you problems. 4 bytes will be allocated for id for text "N/A" and null terminator. Doing this will pop up the id memory area and start corrupting the memory of your program.

In addition, id is allocated on the stack. When returning a pointer from something that is allocated on the stack, the result is that you have a pointer to an unallocated memory area, ready to be arbitrarily overwritten by something else.

What you want seems to be putting a string together with all the ids of visitors and employees who have a time greater than or equal to that of the previous visitor or employee or N/A if there is none.

Since int goes from -2147483648 to 2147483647, but you are not interested in the negative side, then you will need 10 characters / bytes in the worst case to store a int value. Adding the comma to separate them and the null terminator from the last, are 11 characters / bytes for each. In the worst case, you will put the ids of all employees and visitors.

Having a separate function to do concatenation turns out to be a lot simpler than doing all of this within the primeiraPessoa function.

So you can do this:

void concatenar_id(char *c, int id) {
    char temp[11];
    sprintf(temp, "%d", visitante[i].id);
    if (c[0] == 'N') {
        strcpy(c, temp);
    } else {
        strcat(c, ",");
        strcat(c, temp);
    }
}

char* primeiraPessoa(int countV, int countF, Visitante *visitante, Funcionario *funcionario) {
    int hora = 0;
    char *ids = (char *) malloc(11 * (countV + countF));
    strcpy(ids, "N/A");

    for (int i = 0; i < countV; i++) {
        if (i == 0 || hora >= visitante[i].horaE) {
            concatenar_id(ids, visitante[i].id);
            hora = visitante[i].horaE;
        }
    }

    for (int i = 0; i < countF; i++) {
        if (hora >= funcionario[i].horaE) {
            concatenar_id(ids, funcionario[i].id);
            hora = funcionario[i].horaE;
        }
    }

    return ids;
}

In your escreveFicheiro function, you must free the memory allocated with malloc :

void escreveFicheiro(Visitante *visitante, Funcionario *funcionario, int countV, int countF, int countInvalidas, int countBarradas) {
    char nome[] = "estatisticas.txt";
    FILE * fp = fopen(nome, "w");

    if (fp) {
        char *ids = primeiraPessoa(countV, countF, visitante, funcionario);
        fprintf(fp, "15 - %s\n", ids);
        free(ids);
        fclose(fp);
    }
}

In this last function, you are not using the parameters countInvalidas and countBarradas .

    
21.06.2018 / 21:42