我有这个文件并将其作为 c 中的结构数组输入。但是我在将结构成员传递给函数时遇到问题。错误:第 58 行中的下标甚至没有指针或数组值。我是 c 的新手,并且在这个问题上坚持了一周。
代码:
#include <stdio.h>
#include <math.h>
#define SIZE 100
typedef struct list{
int counter;
int year;
double maxrain;
double rank;
} data;
double avg (struct list s, int count);
int main()
{
data a[SIZE];
struct list s;
double sum = 0;
int totalFile = 1; // according to number of csv file
int z, i;
char fName[16];
FILE*fpt;
double mean;
/* reading multiple file */
for (z=1; z<=totalFile; z++)
{
sprintf(fName," ",z);
fpt = fopen(fName,"r");
if(fpt == NULL){
printf("Error opening %s\n",fName);
return(-1);
}
printf("---Reading from file %d---\n", z);
sum = 0;
i = 0;
while(i <= SIZE && fscanf(fpt, "%d%*c%d%*c%f%*c%f", &a[i].counter, &a[i].year, &a[i].maxrain, &a[i].rank) != EOF){
sum = sum + a[i].maxrain;
i++;
}
mean = avg(a[i].maxrain, i);
printf("%f", mean);
return 0;
}
}
double avg(struct list s , int count)
{
double ave;
int i = 0;
for(i=0; i<count; add += s.maxrain[i++]);
ave = add/count;
return ave;
}