Structures

A Structure is a collection of simple variables. The variables of a structure can be of different types: int, float and so on. (this is unlike the array, in which all the elements must be of the same type.) the data items or varible declared in the structure are called the members of the structure. The structure name is often reffered to as its tag.

For C++ programmers, structures are one of the two important building blocks in the understanding of objects and classes.

Defining a Structure

We know that structure is a collection of logically related variables referenced under a single name. These variables can be of different type, and each has a name which is used to select it from the structure.

A structure can be defined as a new named type, thus extending the number of avaialble types.

The keyword struct tells the compiler that a structure is being defiend. Here it is :

struct time

{

short int hh;

short int mm;

short int ss;

};

In the above definition, the identifier time is a structure tag and it identifies this particular data structure and its type specifier.

This definistion of the structure doesn't occupy any memory as no structure variable has been defined. Only the form has been defiend by defiening a structure.

A structure variable having the data form as defined by time can be declared thus:

time arrival_time;

The above declaration declares a structure variable arrival_time of type time. thus the complete structure definition is as follows:

struct time

{

short int hh;

short int mm;

short int ss;

};

time arrival_time;

Now the structure arrival_time has its members (elements) as hh, mm and ss. The C++ compiler automatically allocates sufficent memory to accomodate all of the members that make up a structure variable.

The two seperate statements of structure given above can be joined as shown below:

struct time

{

short int hh;

short int mm;

short int ss;

}arrival_time;

This statement defines a structure type called time and declares a structure variable arrival_time.

More than one structure variable can be defined while defining a structure. For example

struct time

{

short int hh;

short int mm;

short int ss;

} arrival_time, departure_time;

defines a structure type called time and declares two structure variables arrival_time and departure_time of that type.

Referencing Stuccture members (Elements )

Once a structure variable has been defiened, its members (elements) can be accessed using dot operator. For example, the following code assigns 30 to the element ss of the stucture variable arrival_time defiend earlier.

arrival_time.ss=30;

The structure variable followed by a period( . ) and the element name refer that individual structure element. The syntax for accessing a structure element is

<structure-name>,<element-name>

The members of the structure are treated just like other variables. therefore, to print the elemet ss, we can write:

cout << arrival_time.ss

To read the elemets hh, mm and ss of the structure variable tea_time we can write:

cin >> tea_time.hh>>tea_time.mm>>tea_time.ss;

HOME LEARN C++ PREVIOUS NEXT