Structures


The following topics are covered in this section:


Structures are user defined data types. Structures were used in C and they consist of different data types that are related logically in some way. In other words, a structure groups together different data types under one name. The data items that come under a structure are called structure elements or members (or fields). A structure basically puts different data types into one package.

Even for structures you have a declaration and a definition. Declaration means telling the compiler what type a particular variable is. Definition means that the compiler allots memory space to the variable that was declared.

In the case of structures, a structure declaration forms a template. When a structure variable is defined the compiler automatically allocates sufficient memory to accommodate all of its elements. You can define as many structure variables as you want.

The structure syntax is:

‘struct’ is a keyword and it has to be used to declare a structure. Following the keyword, we type the name of the structure (this is also known as the tag-name). Then we have an open brace, followed by the different data types that you want to put in the structure. Finally we add a closing brace and can give the names of the structure variables. This is known as the structure definition.

First of all you should know the use of structures before proceeding further. Suppose you own a small shop. For every set of purchases by a customer, you give the customer a bill. This bill has the bill number and the total amount of purchase mentioned in it. The point to be noted is that, every bill will have a bill number and a particular amount mentioned in it.

Now suppose you want to keep a record of all the bills you have issued through your computer, you will have to store the following in your computer: the bill number and the amount for each bill issued. Bill numbers will be integers whereas the amount to be paid will be a float quantity. Hence, you have two different data types but they are logically related (since every bill will consist of the two data types). It is here that the concept of structures will be found useful.

In the above example, we declare a structure by the name ‘bill’. This contains data items ‘billnumber’ (which is an integer) and ‘amount’ (which is a float quantity). The braces are closed and then we define the variables of the structure, i.e. ‘one’ and ‘two’. This means that ‘one’ and ‘two’ belong to the structure ‘bill’. Both ‘one’ and ‘two’ have their own individual ‘billnumber’ and ‘amount’. Therefore, you can give the bill number and amount for bill ‘one’. Similarly, you can also give the details for ‘two’.

Remember that at the end of the structure declaration and definition, we have to terminate it using the statement terminator.

‘one’ and ‘two’ are known as structure variables.
‘billnumber’ and ‘amount’ are known as the structure elements.
For normal built in data types we would declare a variable as:
    int x;

Where x is the variable and int specifies the type of a variable. Similarly in structures, the variable is declared as belonging to the data type that you have created (which in turn is a combination of different built-in data types).

Instead of this we can write it as follows:

Both the methods are equivalent and are acceptable.

Accessing and Initializing Structure Elements:

Every structure variable that you define will have its own individual elements. So how do we access these individual elements of a structure? To access a particular structure element we make use of the dot operator.

The syntax is:

structure-variable . structure-element

For example:

one.billnumber

is the syntax for accessing the bill number of structure variable one.

Now that you know how to access a structure element you can initialize it as well. Initializing means giving a value to the element.

Example:

Bill ‘one’ has a bill number 214 and the ‘amount’ of the bill is 1032.25
One structure variable can be assigned to another only when they are of the same structure type.

is a valid statement because both ‘one’ and ‘two’ belong to the same structure ‘bill’.
This statement assigns the values of one.billnumber and one.amount to two.billnumber and two.amount respectively.

An example using Structures concept:

The output would be:

Remember: Arithmetic operator works only with built-in data types. Structures are user defined data types.

Hence it is not possible to say: a3 = a1 + a2;

when a1 and a2 are structure variables even if they belong to the same structure type.


Nesting of Structures

Let us add one more entry column to our phonebook structure. This entry will be the birthday of the person. Birthday means that we should store the date, month and year of birth. To illustrate nesting we can group date, month and year into another structure. Check out the program below:

The output of the program is:

First of all have a look at the figure below:

Within the phonebook structure we make use of another structure (date). This is called as nesting of structures.

date birthday;

will declare a structure variable ‘birthday’ belonging to the structure ‘date’.

Thus ‘date’ will have three member elements namely ‘day’, ‘month’ and ‘year’. Since ‘birthday’ comes under the ‘phonebook’ structure we can refer to the member ‘day’ by coding:

a1.birthday.day

The rest of the program should be easy to follow. Now what do you think will be the size of ‘a1’. Size of a1 = 40 (char array) + 40 (char array) + 4 (integer) + size of date structure (which is 12 bytes). Therefore size of a1=96 bytes.

We’ll take a look at two more data types that are less frequently used. It’s always better to know what are the various options available because there could be certain instances where they could prove useful.


Go back to the Contents Page


Copyright © 2004 Sethu Subramanian All rights reserved.