Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Grade {
char *firstname;
char *lastname;
int *grades;
} Grade;
typedef struct Course {
char *name;
char *lastname;
int *studentfinal;
} Course;
void printname ( Grade *t);
void printcourseavg (Course *s);
int main (void){
Grade student;
student.firstname = "John";
student.lastname ="Doe";
student.grades = (int *) calloc(5,sizeof(int));
student.grades [ 0 ] = 50;
student.grades [ 1 ] = 70;
student.grades [ 2 ] = 60;
student.grades [ 3 ] = 75;
student.grades [ 4 ] = 80;
Course final;
final.studentfinal = (int*) calloc(5,sizeof(int));
final.studentfinal [ 0 ] = 78;
final.studentfinal [ 1 ] = 87;
final.studentfinal [ 2 ] = 85;
final.studentfinal [ 3 ] = 86;
final.studentfinal [ 4 ] = 85;
printname( &student);
printcourseavg(&final);
return 0;
}
void printname ( Grade *t )
{
int i;
int total = 0;
float avg;
Grade student;
printf("%s\n", t->firstname);
printf("%s\n", t->lastname); /*
student.grades = (int *) calloc(5,sizeof(int)); */
for ( i = 0; i < 4; i++ ){
total += student.grades [ i ];
}
printf("The average grade for the course is %f\n", avg = total / 5 );
}
void printcourseavg ( Course *s) {
int i,
total = 0;
float avg;
Course final;
/*
final.studentfinal = (int*) calloc(5,sizeof(int));*/
for ( i = 0; i < 5; i++ ) {
total += final.studentfinal [ i ];
}
printf("The course average is %f", avg = total / 5 );
}
John
Doe
Segmentation fault
With callc in functions commented out.
John
Doe
The average grade for the course is 0.000000
The course average is 0.000000j
With calloc working in function? I don't understand why my arrays aren't getting to my functions if my John Doe is printing.