C - Structure Types and Structure Variables


You can define a structure type and a variable of a structure type in separate statements.

struct Dog
  int age;
  int height;
  char name[20];
  char father[20];
  char mother[20];

struct Dog d1 = {4, 7,"A", "B", "C"};

The first statement defines the structure tag Dog, and the second is a declaration of one variable of that type, d1.

The members of the d1 structure tell you that the father of d1 has the name "B" and his mother's name is "C".

You can also declare multiple structure variables in a single statement.

struct Dog d2, d3;

You can remove struct every time you declare a variable by using a typedef definition.

For example:

typedef struct Dog Dog;

This defines Dog to be the equivalent of struct Dog. You can define a variable of type Dog like this:

Dog d3 = {30, 15, "A", "B", "C"};