struct student this_student
is a bit clumsy.
We think of
this_student
as having the type of
struct student
, but all the other types we have
are just a single word.
(There are modifiers that we can apply making the whole think
more than a single word, though.)
In order to give these new types we create the same status
as the built-in types
in the appearance of the code, we have the
typedef
declaration.
We could, for example, have the declaration:
typedef struct student STUDENT;
STUDENT
as in
STUDENT this_student;
We can also dispense with the structure tag altogether as in
typedef struct { char name[30]; char address[60]; int extension; int grad_year; float gpa; } STUDENT;
It turns out that we can also that we can use
typedef
to create
synonyms for any types in our program, not just the structures.
For example, we frequently find lines like:
#define TRUE 1 #define FALSE 0 typedef char BOOLEAN;
BOOLEAN
that
we'll be using to store values that are either
TRUE
or
FALSE
.
So we could declare a variable like:
BOOLEAN is_valid;
is_valid = TRUE;
TRUE
and
FALSE
to match what the conditional expressions in C do, we could then
perform a test like:
if(is_valid)