【问题标题】:note: expected 'float *' but argument is of type 'int *'注意:预期为“float *”,但参数类型为“int *”
【发布时间】:2017-06-14 00:36:08
【问题描述】:

我有一个警告 注意:预期为“float *”,但参数类型为“int *” 我的程序是

#include <stdio.h> //header file

void foo(float *);

int main()
{
    int i = 10, *p = &i;
    foo(&i);
}

void foo(float *p)
{
    printf("%f\n", *p);
}

输出为 0.000 000 为什么?

【问题讨论】:

  • 警告文本不是已经很明显地说明出了什么问题吗?
  • 你没看懂的警告是怎么回事?

标签: c types floating-point int


【解决方案1】:

foo 函数需要int * 类型的参数(指向整数的指针),而不是float * 类型的参数(指向浮点数的指针)。将数据类型 float 更改为 int 并替换为 "%d" 而不是 "%f" 以消除警告。

【讨论】:

    【解决方案2】:

    正如警告所说,您将一个指向 int 的指针传递给 foo(),但 foo() 需要一个指向 float 的指针。所以,要么将i声明为float,要么将foo()的参数改为int*。

    【讨论】:

      猜你喜欢
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 2021-07-09
      • 2013-12-16
      相关资源
      最近更新 更多