Records and structs

A record is a package of variables, possibly of different types. Each variable is a field of the record.  In C language, records are called structs, a shorthand for structure.

Table of contents:

Definition and manipulation of structs

The following example declares (that is, creates) a record  x  with three fields that can be used to store calendar dates:

struct {
   int day;
   int month;
   int year; 
} x; 

It is a good ideia to give a name to the class of all records of a given kind. In our example, the name dmy seems appropriate:

struct dmy {
   int day;
   int month;
   int year; 
}; 
struct dmy x;  // a record x of the dmy kind
struct dmy y;  // a record y of the dmy kind

To refer to a field of a record, just write the name of the record and the name of the field separated by a period:

x.day = 31;
x.month = 12;
x.year = 2020;

Records can be treated as a new data type. After the following definition, for example, we can begin saying date in place of struct dmy:

typedef struct dmy date; 
date x, y; 

Example.  The following function computes the end date of an event upon receiving the beginning date of the event and its duration in days.

date eventend (date beginnig, int duration) {
   date end;
   . . .
   . . .
   end.day = ...
   end.month = ...
   end.year = ...
   return end;
}

The code was omited because it is rather tedious, since it has to account for the number of days in different months and take into account the leap years.  Here is how the function eventend could be used:

int main (void) {
   date a, b;
   scanf ("%d %d %d", &a.day, &a.month, &a.year);
   // &a.day means &(a.day)
   int duration;
   scanf ("%d", &duration);
   b = eventend (a, duration);
   printf ("%d %d %d\n", b.day, b.month, b.year);
   return EXIT_SUCCESS;
}

Exercises 1

  1. Complete the code of the eventend function.
  2. Write a function that decides whether a given dmy struct represents a valid date.
  3. Write a function that receives two dmy structs, each representing a valid date, and returns the number of days that elapsed between the two dates.
  4. Write a function that will receive an integer representing a time interval measured in minutes and return the equivalent number of hours and minutes (for example, 131 minutes is equivalent to 2 hours and 11 minutes). Use a struct as the following:
    struct hm {
       int hours, minutes;
    };
    

Structs and pointers

Every record has an address in computer memory.  (You may assume that the address of a record is the address of its first field.)  The address of a record can be stored in a pointer. We say that such a pointer points to the record.  For example,

date *p; // p is a pointer to dmy records
date  x;
p = &x; // now p points to x
(*p).day = 31; // same effect as x.day = 31

The expression  p->day  is a very useful shorthand for (*p).day. Hence, the last line of the code above could be written as

p->day = 31;

By the way, the meaning of  (*p).day  is very different from that of  *(p.day),  which is equivalent to  *p.day due to the precedence rules.

Exercises 2

  1. Define an employee record to store the information (name, surname, birth date, social security number, wages) of a worker in your company.  Define an array of employees to store the data of all the workers.
  2. A rational is any number of the form p/q, where p is an arbitrary integer and q ia a nonnull integer.  It is convenient to represent a rational by a record:
    typedef struct {
       int p, q;
    } rational;
    

    By convention, the q field of every rational is strictly positive and the greatest common divisor of the fields p and q is 1.  Write functions

    • reduce, to receive integers a and b and return the rational that represents a/b;
    • neg, to receive a rational x and return the rational −x;
    • sum, to receive rationals x and y and return the rational that represents the sum of x and y;
    • prod, to receive rationals x and y and return the rational that represents the product of x by y;
    • quo, to receive rationals x and y and return the rational that represents the quotient of x by y.

Questions and answers