×
☰ Menu

Defining a Structure

Structures must be defined first for their format which may be used later to declare structure variables.

 

The general format of a structure definition is as follows:

struct tag_name
{
data_type member1;
data_type member2;
};

 

Example: 

Let us use an example to illustrate the process of structure definition and the creation of structure variables. Consider a student detail consisting of name, address, and pin. We can define a structure to hold this information as follows

struct student
{
char name[50];
char address[50];
int pin;
};

The keyword struct declares a structure that will store the information for three data fields: name, address, and pin. These fields are called structure elements or members. Each member may belong to a different type of data. student is the name of the structure and is called the structure tag. The tag name may be used subsequently to declare variables that have the tag's structure.

Note that the above definition has not declared any variables. It simply describes a format called template to represent information as shown below:

name array of 50 characters

address array of 50 characters

pin integer