Error in pointer return in function

1

Well, I have the following error:

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;
}
    
asked by anonymous 15.05.2015 / 01:38

3 answers

2

I was able 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");
}
    
15.05.2015 / 06:19
1
char *resultado[30];
static char *str[30];

resultado and str are 30-point arrays! None of the 30 pointers in any of the arrays points to a valid site!

When you use pointers you should always know where they point to.

    
15.05.2015 / 10:32
0

I think by now you should have understood the problem. Anyway, here's my answer: I think, according to what was shown in the previous answer, your error was to return% with% instead of% with%. The first one probably returns an integer, since it refers to a memory address. The second one returns the &str pointer you created in the function. Hope this helps!

    
27.12.2015 / 00:00