One way is to make a constructor to fill in your studentsInfo. This way divides naturally into different source files, where you have one source file to take care of the studentsInfo, etc
Code:
// newStudentDefault : construct a default studentsInfo
studentsInfo newStudentDefault(void);
// newStudent : construct a studentsInfo with the given fields filled out
studentsInfo newStudent(char *studentNumber, char *famName, char *firstName,
char *midName, char *course, int year, int age, char gender,
int final);
// printStudentList : print the list of students
void printStudentList(listOfStudents lis);
int main(void)
{
// Make a lis which is uninitialized
listOfStudents lis;
// Fill element list.student index 0 and 1 using the default struct
lis.student[0] = newStudentDefault();
lis.student[1] = newStudentDefault();
// Fill element lis.student[2] using the customized constructor
lis.student[2] = newStudent("4567", "Doe", "Jane", "X", "History 101",
1980, 0, 'F', 0);
// Set the size member to the proper value
lis.size = 3;
// Print the list
printStudentList(lis);
return EXIT_SUCCESS;
}
The constructors can simply return a pre-initialized struct of that type
Code:
studentsInfo newStudentDefault(void) {
return (studentsInfo) {
.studentNumber = "1234",
.famName = "Doe",
.firstName = "John",
.midName = "X",
.course = "DefaultCourse",
.year = 1980,
.age = 0,
.gender = 'M',
.final = 0,
};
}
studentsInfo newStudent(char *studentNumber, char *famName, char *firstName,
char *midName, char *course, int year, int age, char gender,
int final) {
studentsInfo s = newStudentDefault();
strcpy(s.studentNumber, studentNumber);
strcpy(s.famName, famName);
strcpy(s.firstName, firstName);
strcpy(s.midName, midName);
strcpy(s.course, course);
s.year = year;
s.age = age;
s.gender = gender;
s.final = final;
return s;
}
void printStudentList(listOfStudents lis) {
(void)lis;
// TODO
}
You can also do it with pointers. The only difference is you'd malloc the struct and set the members using the -> notation instead.