Lecture 19: Defining new types and structs


Type

Sometimes it is nice to be able to define a new type of variable to be used later in the program. Just to have a more readable code, or to avoid having to retype code many times. Defining new variable types can be done with the word typedef.
 
 typedef description typename

with typename the name we want to give to the type and decription any type of variable we have learned until now, including arrays, pointers and all the simple variable types.

Examples:

 typedef float real;
This is useful for people that are used to programming in PASCAL. After writing the line above, we can use variables of type real, just like in PASCAL.Variables of 'type' real will be translated by the compiler into variables of type float.

 typedef float floatarray[10];
Defines a new type of variable. The new type is called floatarray and a variable of this type will be equal to an array of 10 floats.

Note that a definition of a new type does not create a variable! It does not save space in memory and it does not assign a name to a variable. It is just a description of a type that we can use later in declaring a variable.
 

... defining new boxes for variables.

Using a new type

After the definition of a type, we can declare variables of this type:
 
 typename varname

with varname the name of a new variable and the type of this variable is typename, as decribed before. After the declaration we can use the variable as if it were declared in the normal way.

Examples:
after the declaration of the new type typedef float real; we can use
 real x;
This looks already much more like PASCAL (in PASCAL it would be "Var x: real")

 floatarray ra;
And in the code we can use this array:
 ra[1] = 2.68;
This is completely equivalent with
 float ra[10];
 ra[1] = 2.68;


More examples

/* example with typedef */

/* definig a new type of variable: */
typedef int ra[6];
/* declare two global variables: */
ra x;
int y[7];

int AreEqual(ra r)
  /* Note that the definition can also be used for parameters */
{
  if (r[0]==r[1])
    return(1);
  else
    return(0);
}

void main()
{
  x[1] = 1;
  x[2] = 0;
  if AreEqual(x)
    printf("First two elements are equal");
  else
    printf("First two elements are different");
  y[1] = 1;
  y[2] = 0;
   /* The following instruction is bad code, because the type of value we pass to
      the function is different than the type of value the function expects: *\
  AreEqual(y);
}



#include <stdio.h>

typedef struct {
  hour, minute, second: integer;
} time;

void showtime(time t);
  /* Will show the time in format h:m:s */
{
  printf("%d:%d.%d\n", t.hour, t.minute, t.second);
}

void main()
{
 time atime;

 atime.hour = 23;
 atime.minute = 16;
 atime.second = 9;
 showtime(atime);
}


struct of structs:

typedef struct {
   int day, month, year;
} date;

typedef struct {
   int hour, minute, second;
} time;

typedef struct {
    time dattime;
    date datdate;
} dateandtime;

dateandtime x;

x.dattime.hour = 1;

x is a variable of type dateandtime which is a struct containing two fields. One field is dattime which is of type time. The other field is of type date. One of the fields of the struct time is hour which is an int.


Quick Test

To test your knowledge of what you have learned in this lesson, click here for an on-line test.

Peter Stallinga. Universidade do Algarve, 30 November 2002