Node:Structure declarations, Next:, Previous:struct, Up:struct



Structure declarations

The following statement is a type declaration, so it belongs with other declarations, either at the start of a program or the start of a code block.

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

The statement says: define a type of variable that holds a string of 100 characters called name, a string of 200 characters called address, and three integers called year_of_birth, month_of_birth, and day_of_birth. Any variable declared to be of type struct personal_data will contain these components, which are called members. Different structures, even different types of structure, can have members with the same name, but the values of members of different structures are independent of one another. You can also use the same name for a member as for an ordinary variable in your program, but the computer will recognize them as different entities, with different values. This is similar to the naming convention for humans, where two different men may share the name "John Smith", but are recognized as being different people.

Once you have declared a type of structure, you can declare variables to be of that type. For example:

struct personal_data person0001;

The statement above declares a variable called person0001 to be of type struct personal_data. This is probably the most common method of declaring a structure variable, but there are two equivalent methods. For example, a structure variable can be declared immediately after the declaration of the structure type:

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