双精度值错误
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));
}
main 方法中的结果给出了转储值,有人可以帮助我吗? 输出:
初始值*
双倍金额* 1000.123400
最终........ 1000.123400
返回---- 1000.123400
format_format_double_trans_amount: /ò# ($$Ð /Òð
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: /ò# ($$Ð/Òð
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在返回导致问题的局部变量的引用。
amount_arra_n 是驻留在堆栈上的字符数组,它在函数调用返回时失效。
You are returning the reference of a local variable which is what causing the problem.
amount_arra_n
is a character array residing on stack which gets invalidated up on return of the function call.函数本地的
amount_array_n
在函数返回后被销毁。使用返回值将调用未定义行为。amount_array_n
being local to the function gets destroyed after the function returns. Using the returned value would invoke Undefined Behaviour.您将在函数 format_double_trans_amount() 末尾返回指向 amount_array_n 的指针,但是该堆栈分配数组的范围仅限于函数体。退出函数后尝试访问该内存区域将导致未定义的行为(最好的情况是显示垃圾,最坏的情况是崩溃)。
对程序的快速而肮脏的修复是将 static 添加到 amount_array_n :
这将使数组成为有效的全局变量。尽管如此,这并不是一个非常优雅的解决方案,只是对您的测试程序的快速修复。
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.
当函数返回时,它分配的字符数组将被释放,因此当您返回指向该数组的指针时,该指针将变得无效。返回它是未定义的行为,这意味着编译器可以做任何事情。如果你真的很不幸,它会起作用,直到几年后你的整个程序崩溃并且你不知道为什么时,你才会意识到你有问题。在大多数情况下,您会得到垃圾值或崩溃。
如果您希望函数返回指向数组的指针,则需要动态分配它,或者传入一个缓冲区(以及缓冲区的大小)以供函数写入。
When your function returns, the character array it allocated is deallocated, so when you return a pointer to that array, the pointer becomes invalid. Returning it is undefined behavior, which means the compiler can do anything. If you're really unlucky, it'll work, and you won't realize you have a problem until years later when your whole program breaks and you have no idea why. In most cases, you get garbage values or crashes.
If you want the function to return a pointer to an array, you need to dynamically allocate it, or pass in a buffer (and the buffer's size) for the function to write into.
amount_array_n 是局部变量,当您将其指针返回到 main 函数时,它不会有有效的地址,这就是您获得该输出的原因。
amount_array_n is local variable, when you return its pointer to main function, it won't have a valid address that's why you get that output.