Warning: assignment makes pointer from integer without a cast

0

I did a search but I did not quite understand what this means and what it says about my code. I have this function cria_palavra that returns a pointer, there in main , when I will receive that return in another function insere_final gives 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]='
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]='%pre%'; // retira o lixo

return *plv;
}
'; // retira o lixo return *plv; }
    
asked by anonymous 22.09.2018 / 09:13

1 answer

0

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

  • Its function cria_palavra returns a char :

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

    But the function insere_lista_final expects a char* :

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

    So you're passing char to a site that expects a char* :

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

    When it is not obvious, start by separating the instructions and do not chain them all together. Separate makes it clearer because it forces you to see the result 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 the problem becomes clearer, as I have separated the instructions.

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

      

    I have this function cria_palavra that returns a pointer

  • The cria_palavra function does not return a word but its first letter. Within the function you have allocated space for the whole word and copied the received text with strcpy but then returns a 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 non-play types.

To fix the two problems you must first 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]='
insere_lista_final(i, li, cria_palavra(i, aux, li));
'; return plv; // sem * }

Then the call is equal to what was that will no longer give error, since it already passes char* to the third parameter as expected:

char cria_palavra(int i, char *aux, Lista *li){
//^--
    
22.09.2018 / 10:33