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.

  Go to Structures in C     Go to Index           Go to Accessing Members