gpt4 book ai didi

c - 参数1.c :4:6: note: expected ‘double *’ but argument is of type ‘double’

转载 作者:行者123 更新时间:2023-11-30 18:22:38 32 4
gpt4 key购买 nike

我对此有疑问:

parameter1.c: In function ‘main’:
parameter1.c:13:2: error: incompatible type for argument 2 of ‘Countcircumferenceofcircle’
parameter1.c:4:6: note: expected ‘double *’ but argument is of type ‘double’

这是代码:

#include <stdio.h>
#define phi 3.14

void Countcircumferenceofcircle(int radius, double *C) {
*C = 2*phi*radius;
}

int main (void) {
int r;
double Circumference;

printf("Insert radius:");
scanf("%d", &r);

Countcircumferenceofcircle(r, Circumference);
printf("Circumference=%f\n", Circumference);

return 0;
}

我需要你的帮助来解决这个问题。

最佳答案

您必须传递变量的地址,因为该函数需要一个指针。结果是函数调用修改了原始变量:

double Circumference;

Countcircumferenceofcircle(r, &Circumference);
// ^^^

// now Circumference is different!

顺便说一句,这是一个相对不优雅和过时的设计。写起来会更干净,效率也不会降低:

double circumference(int radius)
{
return 2.0 * M_PI * radius;
}

int main()
{
// ...
double c = circumference(r);
// ...
}

关于c - 参数1.c :4:6: note: expected ‘double *’ but argument is of type ‘double’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17414911/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com