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
A global variable is a variable that you declare outside any functions and which can be used by any functions defined after the variable declaration:
Most of the time, you don't want to use global variable because you do not really know what happens to them when calling various functions (see the example above where you don't know if the value of a has been modified by
f2
orf3
).A local variable can only be used in the "scope" where it has been declared:
In your case, you are declaring two global variables, but you do not use them globally (because you declare variable with the same name locally, see below), so you should simply declare them locally inside your main:
Note that when you did:
You were declarting type
struct student_s
and in the same time the variablestudent
, while if you remove thestudent
at the end, you are still declaring thestruct student_s
type but not thestudent
variable any more.In your code, you were doing the following:
The output will be
5
, not8
, because inf
the namea
correspond to the parameter of the function, not to the global variable.There is not much to it. Just declare them in main. There are no tricks.
Not directly related to your question, but anyway you should avoid this:
But rather write this:
Both methods are actually equivalent, but with the second method you clearly separate the definition of
struct student_s
from the declaration of the two variablesstudent
andanotherStudent
. This is more readable.Using array:
Well, first replace this:
With:
(That is, remove
student
in the last line).This change is necessary because you want to define the structure type so that you can later define variables of type
struct student_s
, but you don't want to define thestudent
variable here, as that would make it global.Then remove this line:
And finally, declare both variables in
main()
before using them:At the top of the file you have a
struct student_s {...} student;
.This both defines a structure, and allocates one variable of it,
student
.The next line,
struct student_s anotherStudent;
allocates another variable of it.Because they are not declared inside a function, they are global. To make them local, they must be declared in a function, for example:
which defines a local integer variable
i
.I will not provide the code; it is your homework, but I hope I gave enough clarification for you.