I have a struct in which I'd like to pass its fields to particular functions. For example, one of the fields in my struct is students quiz grades for quiz1, 'quiz1'. I'd like to calculate the average, max, and min of all the quizes.I'd like to create a function for each computation, but I dont know how to pass a particular field of the struct to a given function. here is what I have:
The struct:
struct studentData {
char name[30];
int quiz1; int quiz2;
int quiz3; int quiz4;
int mid1; int mid2;
int finalexam;
} ;
the average function:
double calcFAverage(struct studentData *record, int reccount)
{
float sum_F,F_avg;
int k;
// calculate the score sums
for (k=0; k<reccount; k++)
{
sum_F += record[k].finalexam;
}
F_avg = sum_F/reccount;
return F_avg;
}
in the main:
struct studentData record[100];
calcFAverage(record,reccount);
The reccount variable holds the number of records for the struct. But, as you can see the average function is specific only to the final exam scores. How can I make it so that I can pass any of the fields in the struct and get its average. Right now I have an average function for each field which is really a bad way of doing it I think.
*((int*)((char*)&record[k]+offset)). If you use C++, you can pass a pointer to class member like &studentData::mid1