1

I was wondering if someone could help clarify the calculate function on pset5 speller.c (I've copied it below). I'm mostly having difficulty understanding what "->" is exactly doing. Thanks!

double calculate(const struct rusage* b, const struct rusage* a) 
{ 
    if (b == NULL || a == NULL) 
    { 
        return 0.0; 
    } 
    else 
    { return ((((a->ru_utime.tv_sec * 1000000 + a->ru_utime.tv_usec) -
         (b->ru_utime.tv_sec * 1000000 + b->ru_utime.tv_usec)) +
         ((a->ru_stime.tv_sec * 1000000 + a->ru_stime.tv_usec) -
         (b->ru_stime.tv_sec * 1000000 + b->ru_stime.tv_usec))) / 1000000.0); 
    } 
}
2

Both a and b are pointers to structures. The -> is not so much an operator as a selector. It allows you to select a specific element from the structure, as in a->ru_utime.tv_sec BTW, the dot in the middle serves a similar function.

These are topics that you will see in later psets, so I'll leave the explanation to Prof. Malan's lectures.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .