Node:Structure declarations using typedef, Previous:Structure declarations, Up:Structure declarations



Structure declarations using typedef

Alternatively, the typedef command can be used to cut down on typing out code in the long term. The type definition is made once at the start of the program and subsequent variable declarations are made by using the new name, without the word struct:

typedef struct
{
  char name[100];
  char address[200];
  int year_of_birth;
  int month_of_birth;
  int day_of_birth;
} personal_data;

personal_data person001;
personal_data person002;
personal_data person003;

Note that this use of the typedef command parallels the usage we have already seen:

typedef existing_type new_type

In the example above of using typedef to declare a new type of structure, the metasyntactic variable new_type corresponds to the identifier personal_data, and the metasyntactic variable existing_type corresponds to the following code:

struct
{
  char name[100];
  char address[200];
  int year_of_birth;
  int month_of_birth;
  int day_of_birth;
}

Structure type and variable declarations can be either local or global, depending on their placement in the code, just as any other declaration can be.