I am currently working on a program that takes input from a file and calculates the averages. I decided to just write the program they way I "thought" I could do it.

struct student{
    char name[15];
    double average;
    double score1,score2,score3;
};


struct student num_students[50];

int main (){

  int i = 1; /* Array Counter */
  int total = 0; /* Counter for Total Students */
  FILE *input;

  input = fopen ("grades_file","r");
  if ( input == NULL ){
      perror ("The File grades_file could not be opened\n");
  }
  while ( feof(input)==  0){
    fscanf ( input, "%s", num_students[i].name );
    fscanf ( input, "%lf", &num_students[i].score1 );
    fscanf ( input, "%lf", &num_students[i].score2 );
    fscanf ( input, "%lf", &num_students[i].score3 );
    num_students[i].average = (((num_students[i].score1)+(num_students[i].score2)+(num_students[i].score3))/3);

    i++;
    total++;
  }

  for ( i = 1; i < total; i++){
      printf("%s ", num_students[i].name);
      printf("%.2lf\n", num_students[i].average);
  }


return 0;
}

So, now I have the data I need, but I can't wrap my head on how to pass the average member through a function. So I guess my question is: Can you assign a pointer to a member in a structure? Or am going in the wrong direction?

Thanks,

twburkle

I don't understand your question: what's a function? Why and where "assign a pointer to a member in a structure"?
It seems this program face is familiar ;). If you want to compute a student grade, pass a pointer to a student structure:

/* I hate struct name typenames */
typedef struct student
struct student {
    char name[15];
    /*double average;*/
    double score1,score2,score3;
} Student;
/* No need in average member in Student! */
double average(const Student* ps)
{
    return (ps->score1+ps->score2+ps->score3)/3.0;
}

char grade(const Student* ps)
{
    char g = 'Z';
    double ave = average(ps);
    if (ave >= 90)
        g = 'A';
    ....
    return g;
}
/* and so on: no need in globals! */
int main()
{
    Student students[50];
    ...
    for (i = 0; i < total; ++i)
        printf("%s: %c\n",students[i].name,grade(students+i));
    ...
    return 0;
}

Apropos, array indicies in C started from 0 for the present /* see a strange comment after i = 1: Array Counter */...
No need in i++ and total++: use total as an index in while loop then increment it, or better write simple and clear for loop:

for (total = 0; !feof(input); ++total)
{
    ...
    fscanf (input, "%s", students[total].name);
    /* Awful call: try to input name Struct's&PointerQuestionAuthor */
    /* Don't follow my advice above w/o parachute... */
    ...
}

Thanks for the reply, I think I know what to do now ^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.