Next: Accessing Members of Up: Structures in C Previous: Structures in C

Defining a Structure

A structure type is usually defined near to the start of a file using a typedef statement. typedef defines and names a new type, allowing its use throughout the program. typedefs usually occur just after the #define and #include statements in a file.

Here is an example structure definition.


  typedef struct {
          char name[64];
          char course[128];
          int age;
          int year;
  } student;

This defines a new type student variables of type student can be declared as follows.


  student st_rec;
Notice how similar this is to declaring an int or float.

The variable name is st_rec, it has members called name, course, age and year.


craa27@strath.ac.uk
Tue Jan 17 11:40:37 GMT 1995