"[Error] pointer value used where a floating point value was expected" 如何解决此错误?

"[Error] pointer value used where a floating point value was expected" How to solve this error?

本文关键字:value 何解决 解决 expected 错误 point Error pointer used floating where      更新时间:2023-10-16

我的代码在下面我想在以下代码中将华氏度转换为摄氏度,但是我会遇到错误吗?有人知道如何解决此错误吗?

#include <stdio.h>
int main()
{
    int frnhet[]={0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300};
    double celcius;
    int i;
    for(i=0;i<16;i++)
        {
      celcius = ((float)(5/9) + (float)(frnhet-32));
        printf("celcius = %f",celcius);
       }
     return 0;
 }

您错过了代码中的frnhet索引。请参考以下代码 -

#include <stdio.h>
    int main()
    {
        int frnhet[]={0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300};
        double celcius;
        int i;
        for(i=0;i<16;i++)
            {
          celcius = ((float)(5/9) + (float)(frnhet[i]-32));
            printf("celcius = %f",celcius);
           }
         return 0;
     }

请更改行

celcius = ((float)(5/9) + (float)(frnhet-32));

to

celcius = ((float)(5/9) + (float)(frnhet[i]-32));

您错过了访问frnht数组的索引

#include <stdio.h>
int main()
{
    int frnhet[]={0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300};
    double celcius;
    int i;
    for(i=0;i<16;i++)
        {
      celcius = ((float)(5/9) + (float)(frnhet[i]-32));
        printf("celcius = %f",celcius);
       }
     return 0;
 } 

celcius = ((float)(5/9) + (float)(frnhet-32));中,您将通过指针将其传递给浮点值除外。用于传递值,请使用celcius = ((float)(5/9) + (float)(frnhet[i]-32));

frnhet是一个数组,在表达式frnhet-32中,它将转换为指针转换为其第一个元素。将frnhet-32更改为frnhet[i]-32

celcius = (5.0/9 + frnhet[i]-32);

您的错误在这里:

celcius = ((float)(5/9) + (float)(frnhet-32));

它必须是 frnhet[i]

celcius = ((float)(5 / 9) + (float)(frnhet[i] - 32));