C函数计算不正确

时间:2016-06-07 15:59:47

标签: c function pointers

在我设置*area = 2或其他任何int的函数中,我的程序按预期工作。出于某种原因,我无法通过宽度计算长度并将其正确放置,我得到的只是0' s。我错过了什么?这是电话中的内容吗? %f也是double的适当转换,还是有更好的用途?我的代码如下:

#include <stdio.h>

void area_perimeter(double width, double length, double *area, double *perimeter);

    int main(){

            double width, length, are, peri;

            printf("Enter the width of the rectangle: ");
            scanf("%f",&width);

            printf("Enter the length of the rectangle: ");
            scanf("%f",&length);

            area_perimeter(width, length, &are, &peri);

            printf("The area of the rectangle is: %f\n",are);
            printf("The perimeter of the rectangle is: %f\n",peri);

            return 0;
    } 

    void area_perimeter(double width, double length, double *area, double *perimeter){

            *area = length * width;

            *perimeter = (*area * 2);

    }

1 个答案:

答案 0 :(得分:4)

您通过将指针传递给%f scanf() float*来自double*的{​​{1}}对象来调用未定义行为,但是您通过了%lf }。使用%f读取双倍。

printf()中使用double打印%lf很不错。 C99编译器也将接受#include <stdio.h> void area_perimeter(double width, double length, double *area, double *perimeter); int main(void){ double width, length, are, peri; printf("Enter the width of the rectangle: "); if (scanf("%lf",&width) != 1) { fputs("read error width\n", stderr); return 1; } printf("Enter the length of the rectangle: "); if (scanf("%lf",&length) != 1) { fputs("read error length\n", stderr); return 1; } area_perimeter(width, length, &are, &peri); printf("The area of the rectangle is: %f\n",are); printf("The perimeter of the rectangle is: %f\n",peri); return 0; } void area_perimeter(double width, double length, double *area, double *perimeter){ *area = length * width; *perimeter = (*area * 2); }

试试这个:

d3.csv("../hello.csv", function(data) {
    console.log(data);
    var all = [];
    data.map(function (d) {
        Array.prototype.push.apply(all, d.description.split(" "));
    });
    console.log(all);
});