我正在尝试将argv [1]转换为浮点值但最终出现编译错误。这是我的代码。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
double circleArea = 3.14159 * atof(argv[1]) * atof(argv[1]); //this works
double circleArea = 3.14159 * (double) argv[1] * (double) argv[1]; //this does not
return 0;
}`
为什么第一个语句编译而第二个语句不编译? (double) argv[1]
不应该与atof(argv[1])
一样有效吗?
更新:这些是我得到的错误:
circleArea.c: In function ‘main’:
circleArea.c:9:2: error: pointer value used where a floating point value was expected
double area = 3.14159 * (double) argv[1] * (double) argv[1]; //this does not
^~~~~~
circleArea.c:9:2: error: pointer value used where a floating point value was expected
答案 0 :(得分:0)
您看到的问题与演员/转化有关。在第二行 - 你得到错误 - 你需要将字符串转换为Groovy Shell (2.5.0-beta-1, JVM: 1.8.0_152)
Type ':help' or ':h' for help.
----------------------------------------------------------------------------------------------
groovy:000> test = ["a", "b", "c"]
===> [a, b, c]
groovy:000> test.inspect()
===> ['a', 'b', 'c']
,但你不是 - 你只是在转换值,即&#34;告诉编译器你在那里有什么是double
&#34;,而不是。
在C语言中,类似类型的投射适用于double
到int
,float
到short
,double
到float
,{{1到long
。当然,在某些转换中你可能会失去精确度。但是,例如,你不能将char
转换为int
(实际上,你可能,但你肯定有意想不到的结果),因为一个是数组,另一个是单个变量
要使其正常工作,您应该使用int []
功能。查看this post了解详情。