Pointer return error in function

Asked

Viewed 264 times

1

Guys, I’m making the following mistake:

incompatible types when assigning to type 'char [30] ' from type 'char *'

the code is as follows::

    int main()
{
    char *resultado[30];
    float valor = 12735.98;
    resultado=monet(valor);  \\O AVISO DE ERRO É AQUI
    printf("%s \n",resultado);

}

char *monet(float v){
    static char *str[30];
    sprintf(str,"R$ %2f",v);
    return &str;
}
  • I can help you, I need to understand your reasoning a little better, give me some information about what you intend to do.

  • Related question: http://answall.com/questions/16942

3 answers

2


I managed to print here:

char *monet(float v){
    static char str[30];
    sprintf(str,"R$ %.2f",v);
    return str;
}


int main(int argc, char *argv[]){
    char *resultado;
    float valor = 12735.98;
    resultado=monet(valor); 
    printf("%s \n",resultado);

    system("pause");
}

1

char *resultado[30];
static char *str[30];

resultado and str are arrays of 30 pointers! None of the 30 pointers of any of the arrays points to a valid place!

When you use pointers you must always know where they point.

0

I think you’ve probably figured it out by now. Anyway, here’s my answer: I think, according to what was shown in the previous answer, your mistake was to return &str instead of str. The first one probably returns an integer, since it refers to a memory address. The second one returns the pointer str that you created in the function. I hope to have helped!

Browser other questions tagged

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