C - Structure struct Introduction

Introduction

The struct keyword defines a collection of variables of various types called a structure.

We can treat structure as a single unit.

Consider the following structure declaration:

struct Dog
{
  int age;
  int height;
} silver;

This example declares a structure type called Dog.

This isn't a variable name; it's a new type.

The variable names within the Dog structure, age and height, are called members or fields.

In the example, an instance of the structure, called silver, is declared at the same time that the structure is defined.

silver is a variable of type Dog.

Whenever you use the variable name silver, it includes both members of the structure: the member age and the member height.

The following code adds more fields to structure type Dog:

struct Dog
{
  int age;
  int height;
  char name[20];
  char father[20];
  char mother[20];
} dobbin = {
             4, 7, "A", "B", "C"
};

Related Topics