Why should we use mediate variable?

Hi, everyone.
My professor said we should use mediate variable when we want to input data to a float type member of a struct variable or an element of 2 dimensional float array.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#define MAX_NAME 20
#define MAX_STUDENT 20
typedef struct
{
    char name[MAX_NAME+1];
    double grade;
} Student;

Student arrayStudent[MAX_STUDENT];

void Input (Student arrayStudent[], int num)
{
    int i;
    double temp;  // mediate variable
    for (i = 0; i < num; ++i)
    {
        printf ("Student %d:\n", i+1);
        printf ("Enter name: ");
        gets (arrayStudent[i].name);
        printf ("Enter grade: ");
        scanf ("%lf", &temp);
        arrayStudent[i].grade = temp;
        while (getchar () != '\n');
    }
}

As you can see, we input data to temp first, then assign value of temp to arrayStudent[i].grade
I wonder why we should do like that? Because when I inputed data to a float type member of a struct variable directly (without using mediate variable) by using statement scanf ("%lf", &arrayStudent[i].grade);, it didn't cause any problem.
Or maybe what my professor said is only right with old version compilers?
Last edited on
Topic archived. No new replies allowed.