我試圖在任何地方搜索,但這很難說出來,這很可能是一個簡單的修復。基本上,當我瀏覽我的計劃,應該計算一年的平均降雨量時,它會出現一個非常大的數字,但是,我認爲這可能只是我做錯算術或語法錯誤某種形式,但事實並非如此,當我檢查函數返回的值是合適的值時。C編程返回值奇數
#include <stdio.h>
#include <string.h>
void getData(float *, float *);
int main()
{
char state[2], city[81];
float rainFall[12], outputAverage, *pAverage;
printf("Name Here\n");
printf("Please enter the state using a two letter abreviation: ");
gets(state);
printf("Please enter the city : ");
gets(city);
pAverage = &outputAverage;
(getData(rainFall, pAverage));
printf("%.2f", outputAverage);
return (0);
}
void getData(float *rainFall, float *pAverage)
{
int i;
float total;
for (i=0; i<12; i++)
{
printf("Please enter the total rainfall in inches for month %d: ", i+1);
scanf("%f", &rainFall[i]);
total += rainFall[i];
}
*pAverage = total/12;
}
CAN你給樣品的輸入和輸出? – 2013-05-03 03:07:40
你正在溢出你的角色數組。例如,你的'state'數組只有兩個字符的空間,但是兩個字符的字符串不可能只適用於兩個字符。 (Pigeonhole原理,如果兩個字符的字符串可以放入兩個字符中,那麼一個字符的字符串也可以放入兩個字符中,但是兩個字符的字符串總數只能等於兩個字符串的數量所以你怎樣才能用兩個字符表示單字符串?因此,你至少需要三個字符的空間來適合兩個字符的字符串。) – 2013-05-03 03:25:02