我想制作一个程序来获取 2 个月中每一天的温度
并打印出每一天的平均值
错误代码在调用day_avg()的那一行。
(不能将 float 转换为 float *)
#include <stdio.h>
void day_avg(float month[],float month2[]);
int main()
{
float jul[31]={ 31,28,31,30,31,30,31,31,30,31,30,31 };
float aug[31]={ 31,28,31,30,31,30,31,31,30,31,30,31 };
day_avg(jul[31],aug[31]);
}
void day_avg(float month[],float month2[]){
int i;
float avg[31]={0};
for(i=0;i<31;i++)
avg[i]=(month[i]+month2[i])/2.0;
for(i=0;i<31;i++)
printf("\nAverage of temperature of 2 months for day %d :%.1f",i+1,avg[i]);
}
最佳答案
您对 day_avg(jul[31],aug[31])
的调用应该是 day_avg(jul, aug)
jul
和 aug
是对数组的引用,它们知道它们有多大,所以在这样的函数调用中指定它是错误的,并没有按照你的想法去做.实际发生的情况是,您发送的是 jul
和 aug
的数组索引 31 中的值,而不是整个数组。
第二个问题是数组索引 31 超出了数组的范围,在大小为 31 的数组中有效索引为 0-30
。
你想发送整个数组,所有天的温度数据,因此 day_avg(jul, aug)
关于具有数组输入 : Error:|Cannot cast float to float*| without any cast 的自定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20857667/