C 学生作业, ‘%f’ 需要类型为 ‘float *’ 的参数,但参数 2 的类型为 ‘double *’

标签 c reference double

<分区>

我正在做作业,我收到了这个警告:

C4_4_44.c:173:2: warning: format ‘%f’ expects argument of type ‘float *’, 
but argument 2 has type ‘double *’ [-Wformat]

变量在 main 中声明为:

double carpetCost;

我将函数调用为:

getData(&length, &width, &discount, &carpetCost);

函数如下:

void getData(int *length, int *width, int *discount, double *carpetCost)

{

    // get length and width of room, discount % and carpetCost as input

    printf("Length of room (feet)? ");

    scanf("%d", length);

    printf("Width of room (feet)? ");

    scanf("%d", width);

    printf("Customer discount (percent)? ");

    scanf("%d", discount);

    printf("Cost per square foot (xxx.xx)? ");

    scanf("%f", carpetCost);

    return;

} // end getData

这让我抓狂,因为书上说你不要在

scanf("%f", carpetCost); 

当从传递它的函数访问它时,它是引用。

知道我在这里做错了什么吗?

最佳答案

改变

scanf("%f", carpetCost);

scanf("%lf", carpetCost);

%f 转换规范用于 float * 参数,您需要 %lf 用于 double *争论。

关于C 学生作业, ‘%f’ 需要类型为 ‘float *’ 的参数,但参数 2 的类型为 ‘double *’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21945045/

相关文章:

php - 用PHP调用C程序

php - 通过更改从函数返回的引用来更改全局数组值

c++ - 比较 (int)double 和 (int)int 时出现异常

perl - 当我将 coderef 传递给这个原型(prototype)化的 Perl 子例程时,为什么会出现语法错误?

具有两个指针的 C 函数

ios - 获取小数点后一位 iOS Objective C

c - printf ("%f") 或 printf ("%lf") 对于 c 中的 double ?

c - 如何在 C 中将字符串类型转换为整数并将其存储在整数数组中?

c - 使用指针: program that sorts an integer array,段错误

c - Windows (64) 内核驱动程序和可分页功能