char *format_double_trans_amount(double amount)
{
char amount_array_n[25];
strcpy(amount_array_n,"");
printf("\nInitial value ***** %s",amount_array_n);
printf("\nDouble amount ***** %f",amount);
sprintf(amount_array_n,"%1f",amount);
printf("\nFinal ........ %s", amount_array_n);
printf("\nReturn ---- %s",amount_array_n);
return amount_array_n;
}
int main()
{
printf ("\nformat_format_double_trans_amount: %s ************", format_double_trans_amount(1000.123400));
}
the result in the main method gives dump value could anybody please help me on this?
output:
Initial value *
Double amount * 1000.123400
Final …….. 1000.123400
Return —- 1000.123400
format_format_double_trans_amount: /ò# ($$Ð/Òð
You are returning a pointer to amount_array_n at end of the function format_double_trans_amount(), however the scope of that stack allocated array is limited to the body of the function. Trying to access that memory area after exiting the function will result in undefined behaviour (at best rubbish displayed and at worst a crash).
The quick and dirty fix to your program is adding static to amount_array_n:
That would make the array an effective global variable. Still, not a very elegant solution, just a quick fix for your test program.