【问题标题】:Celcius to fahrenheit with pointers in C摄氏度到华氏度与 C 中的指针
【发布时间】:2014-10-26 14:42:32
【问题描述】:

所以我刚刚了解了指针以及如何在 C 中使用单独的函数制作程序。我将这个华氏度设置为 celcius 程序,但我的编译器给了我一个错误:在我的 printf 中“无效使用 void 表达式”。

我知道这与我的函数 fahrenheit_temperature 有关。但我不明白如何解决它?我试图将功能更改为加倍,但它不起作用。有任何想法吗?谢谢。

我的代码:

#include <stdio.h>

void fahrenheit_temperature(double celcius_temp, double *fahrenheit_temp){
  *fahrenheit_temp = (9.0 / 5.0) * celcius_temp + 32.0;
}

int main(void){
  double fahrenheit_temp;

printf("Freezing point: %6.2f F.\n", 
     fahrenheit_temperature(0.0, &fahrenheit_temp));

printf("Boiling point: %6.2f F.\n",
     fahrenheit_temperature(100.0, &fahrenheit_temp));

return 0;
}

新代码:

#include <stdio.h>

void fahrenheit_temperature(double celcius_temp, double *fahrenheit_temp){
  *fahrenheit_temp = (9.0 / 5.0) * celcius_temp + 32.0;
}

int main(void){
double fahrenheit_temp, celcius_temp;

printf("Enter degrees celcius: ");
scanf("%lf", &celcius_temp);
fahrenheit_temperature(celcius_temp, &fahrenheit_temp);
printf("The converted temperature is: %f\n", fahrenheit_temp);

return 0;
}

【问题讨论】:

  • 看起来好多了,实际上您在函数中正确使用了指针

标签: c pointers function-pointers


【解决方案1】:

fahrenheit_temperature() 被声明为void,因此它不返回任何内容,之后没有任何内容传递给printf()

fahrenheit_temperature() 返回后打印变量fahrenheit_temp。将fahrenheit_temp 传递给printf() 而不是fahrenheit_temperature

然后您可以使用函数的返回值来指示失败/误用。

#include <stdio.h>
#include <errno.h>

#define TEMP_C__TO__TEMP_F (temp_c) \
  ((9.0 / 5.0) * (temp_c) + 32.0)

#define ABSOLUTE_ZERO_TEMP_C (-273.15)
#define ABSOLUTE_ZERO_TEMP_F TEMP_C__TO__TEMP_F(ABSOLUTE_ZERO_TEMP_C)
#define INVALID_TEMP_F (ABSOLUTE_ZERO_TEMP_F - 1.)

int fahrenheit_temperature(double celcius_temp, double *fahrenheit_temp) 
{
  int result = 0;

  if (NULL == fahrenheit_temp)
  {
    result = -1;
    errno = EINVAL;
  }
  else if (ABSOLUTE_ZERO_TEMP_C > celcius_temp) /* It cannot be colder then −273.15 C. */
  {
    result = -1;
    errno = EINVAL;
  }
  else
  {
    *fahrenheit_temp = ABSOLUTE_ZERO_TEMP_C(celcius_temp);
  }

  return result;
}

int main(void)
{
  double fahrenheit_temp = INVALID_TEMP_F;

  if (-1 == fahrenheit_temperature(0.0, &fahrenheit_temp))
  {
    perror("fahrenheit_temperature() failed");
  }
  else
  {
    printf("Freezing point: %6.2f F.\n", fahrenheit_temp);
  }

  /* Test error case 1: */
  fahrenheit_temp = INVALID_TEMP_F;
  if (-1 == fahrenheit_temperature(0.0, -543.21))
  {
    perror("fahrenheit_temperature() failed");
  }
  else
  {
    printf("Test 1 failed: invalid value, should not get here: %6.2f F.\n", fahrenheit_temp);
  }

  /* Test error case 2: */
  fahrenheit_temp = INVALID_TEMP_F;
  if (-1 == fahrenheit_temperature(0.0, NULL))
  {
    perror("fahrenheit_temperature() failed");
  }
  else
  {
    printf("Test 2 failed: invalid value, should not get here: %6.2f F.\n", fahrenheit_temp);
  }

  ...

:-)

【讨论】:

  • 我不想在 fahrenheit_temperature 函数中返回结果,因为我想使用指针 :) 但我已经尝试实现您主要的建议(我认为):这更好吗?
【解决方案2】:

你有一个变量,所以在 printf 中使用这个变量:

#include <stdio.h>

void fahrenheit_temperature(double celcius_temp, double *fahrenheit_temp){
  *fahrenheit_temp = (9.0 / 5.0) * celcius_temp + 32.0;
}

int main(void){
  double fahrenheit_temp;

fahrenheit_temperature(0.0, &fahrenheit_temp);
printf("Freezing point: %6.2f F.\n",fahrenheit_temp);

fahrenheit_temperature(100.0, &fahrenheit_temp);
printf("Boiling point: %6.2f F.\n",fahrenheit_temp);

return 0;
}

那么您不需要更改 fahrenheit_temperature 函数的返回类型,只需保留 printf 语句的函数 OUT - 因为它不返回任何值。

【讨论】:

    【解决方案3】:

    fahrenheit_temperature 返回void,所以printf 被传递void,因此出现错误。

    【讨论】:

      【解决方案4】:

      问题是您需要将值传递给printf。现在你正在通过被调用的函数传递void

      一个可能的最小修复是添加return *fahrenheit_temp;

      #include <stdio.h>
      
      double fahrenheit_temperature(double celcius_temp, double *fahrenheit_temp){
          *fahrenheit_temp = (9.0 / 5.0) * celcius_temp + 32.0;
          return *fahrenheit_temp;
      }
      
      int main(void){
         double fahrenheit_temp;
      
         printf("Freezing point: %6.2f F.\n", 
             fahrenheit_temperature(0.0, &fahrenheit_temp));
      
         printf("Boiling point: %6.2f F.\n",
             fahrenheit_temperature(100.0, &fahrenheit_temp));
      
         return 0;
      }
      

      或者,您可能更喜欢使用本地变量中存储的值:

      #include <stdio.h>
      
      double fahrenheit_temperature(double celcius_temp, double *fahrenheit_temp){
          *fahrenheit_temp = (9.0 / 5.0) * celcius_temp + 32.0;
          return *fahrenheit_temp;
      }
      
      int main(void){
      
          double fahrenheit_temp;
          fahrenheit_temperature(0.0, &fahrenheit_temp);
          printf("Freezing point: %6.2f F.\n", fahrenheit_temp);
      
          fahrenheit_temperature(100.0, &fahrenheit_temp);
          printf("Boiling point: %6.2f F.\n", fahrenheit_temp);
      
          return 0;
      }
      

      【讨论】:

      • 如果我正确理解了指针,我会说 fahrenheit_temperature 函数中的返回是不必要的。有错吗?
      • 这是个人喜好和表现的问题。您可能更喜欢第二种解决方案。但是,由于您正在学习指针,因此我发布了这两种解决方案..
      猜你喜欢
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2017-07-24
      • 1970-01-01
      相关资源
      最近更新 更多