I have written the following code for a project however it fails a singular test asking for two variables to not be global, and instead be local to main()
. Modify structexample1.c
so that the variables student
and anotherStudent
are not global but are local to main. I vaguely understand the local and global concept but I am unsure how to implement what the question is asking into the code I have written.
#include <stdio.h>
#include <stdlib.h>
struct student_s {
char* name;
int age;
double height;
struct student_s* next;
} student;
struct student_s anotherStudent;
void printOneStudent(struct student_s student)
{
printf("%s (%d) %s %s %.2lf %s\n", student.name, student.age, ",", "height", student.height, " m");
}
void printStudents(const struct student_s* student)
{
while (student != NULL) {
printOneStudent(*student);
student = student->next;
}
}
int main(void)
{
student.name = "Agnes McGurkinshaw";
student.age = 97;
student.height = 1.64;
student.next = &anotherStudent;
anotherStudent.name = "Jingwu Xiao";
anotherStudent.age = 21;
anotherStudent.height = 1.83;
anotherStudent.next = NULL;
printStudents(&student);
return EXIT_SUCCESS;
}
I know I will need to define these variables inside main()
but I am unsure how to implement them in a way that does not entirely break my code. The output of the code should remain as follows:
Agnes McGurkinshaw (97), height 1.64 m
Jingwu Xiao (21), height 1.83 m