所以我剛剛瞭解了指針和如何在C中用單獨的函數編寫程序。我將這個華氏轉換成了C語言程序,但是我的編譯器給我一個錯誤:void invalid expression of void expression in我的printf的。Celcius用C中的指針指向華氏溫度
我知道這與我的功能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;
}
這看起來好多了,你其實用ARE指針正確在你的函數 – Grantly 2014-10-26 14:54:45