Warning: assignment makes Pointer from integer without a cast

Asked

Viewed 3,977 times

1

I did some research, but I don’t quite understand what that means and what it says about my code. I have that function cria_palavra which returns a pointer, there in the main, when I will receive this return in another function insere_final give this Warning of the title

int insere_lista_final(int i, Lista* li, char *dt) // parametros da func

insere_lista_final(i,li,cria_palavra(i,aux,li));


char cria_palavra(int i, char *aux, Lista *li){

char *plv;
plv = (char*)malloc(i*sizeof(char));// aloca a palavra
strncpy(plv,aux,i);// passa aux para plv
plv[i]='\0'; // retira o lixo

return *plv;
}

1 answer

2


Let’s start by first realizing the problems you have, and that are all related to the concordance in the types.

  • Its function cria_palavra return a char:

    char cria_palavra(int i, char *aux, Lista *li){
    //^--
    

    But the function insere_lista_final expecting a char*:

    int insere_lista_final(int i, Lista* li, char *dt)
    //                                            ^--
    

    That’s why there’s a char in a place that awaits a char*:

    insere_lista_final(i,li, cria_palavra(i,aux,li));
    //                          ^--- isto devolve char mas espera-se char* no 3 parâmetro
    

    When not evident, start by separating the instructions and not chaining them all together. Separating makes it clearer because it forces you to see the outcome of each thing:

    char palavra = cria_palavra(i,aux,li); //aqui torna-se mais claro que sai um char da função
    insere_lista_final(i, li, palavra);
    

    Here it becomes clearer the problem, because I separated the instructions.

    Notice that this is different from what you understood from the code when it says:

    I have that function cria_palavra which returns a pointer

  • The function cria_palavra does not return a word but its first letter. Within the function you allocated space for the whole word and copied the received text with strcpy but then returns one char which is just a letter with return *plv;. The function type itself is marked as char(letter) instead of char* (pointer to char).

    This second problem affects the first problem of the guys not playing.

To fix the two problems you must first change the function that creates a word to return the correct type:

char* cria_palavra(int i, char *aux, Lista *li) {
//  ^-- agora devolver char* e não char
    char *plv;
    plv = (char*)malloc(i*sizeof(char));
    strncpy(plv,aux,i);
    plv[i]='\0';

    return plv; // sem *
}

Then the call is the same as the one I was on that won’t go wrong, because it’s gone char* in the third parameter as expected:

insere_lista_final(i, li, cria_palavra(i, aux, li));

Browser other questions tagged

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