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

标签 c

我对此有疑问:

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/

相关文章:

c - 如何定义 MPI_Type_struct,其中结构中涉及通用指针

c - C 中的无效初始值设定项

c - 链接两个 C 程序

c - 指针初始化 : address or value

c - 鼠标 Action 的input_event成员解释

c、正确使用线程

c - 如何从 ASM 堆栈中的 C 代码访问双指针

openCv 中的连通分量提取

c - 是否有任何运算符可以用来附加 2 个计算?

c - typedef 指针的大小