Error: Conflicting types for 'first'

Asked

Viewed 640 times

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 function main gives the desired value, but if fprintf() no longer gives this value. How can I solve?

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

}
  • What kind of things have been handed down to primeiraPessoa within the fprintf ? Also put this part in the code to make it clearer

  • I put the function where it is called the first Map and where are the types of the variables

  • Function signature starts with 2 ints, but you’re calling her with int, Visitante*

  • I’m sorry, I changed it a little bit and forgot to change it here too. But the error continued to give

1 answer

0


That one strcat(id,","); will give you trouble. 4 bytes will be allocated to id for the text "N/A" and the null terminator. By doing this you will burst the memory area of the id and start corrupting your program’s memory.

Moreover, id is allocated to 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 to assemble a string with all visitor and employee ids that have a time greater or equal to that of the previous visitor or employee or N/A if there is no.

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

Having a separate function to make the concatenation ends up being much simpler than doing all this within the function primeiraPessoa.

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 his job escreveFicheiro, you must release 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.

  • Thank you! I’ve already applied this in my program, but I got the same problem

  • @Rodrigomarcelino Edit the question and place the entire error message. This Conflicting Type for 'Function' hints that the error is elsewhere.

  • The only thing that is written in the error is: "Error: Conflicting types for "primeiraPessoa'. And as far as you can see from my web search, that error is due to the arguments of the first function

  • @Rodrigomarcelino The function primeiraPessoa is before or after function escreveFicheiro? Should have been earlier.

  • is after... Should be before right? I’ll do it

  • 1

    THANKS!!! That was it! The function has to be before the function where I give the fprintf(); this part of the code was made by a colleague of mine and I don’t know how I didn’t remember it.....

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.