Structures

Introduction :

So far we have come across, in-built data types (like int, char etc…). But structures are user defined data types. Structures consist of different data types that are related logically in some way. Actually a structure groups together different data types under one main structure name. The data items that come under a structure are called structure elements or members.

Even for structures you have a declaration and a definition. I mentioned earlier (see Terms section) about declaration and definition. Declaration means telling the compiler what a particular item is. Definition means that the compiler allots memory space to the item being defined.

Similarly, a structure declaration forms a template. When a structure variable is defined the compiler automatically allocates sufficient memory to accommodate all of its elements. One or more variables can be defined .

Take a look at the structure syntax :

Syntax :

Keyword struct  tag/structure name

{
data type         variable name;
}
structure variables;                                                     // STRUCTURE DEFINITION

struct is a keyword, i.e. to declare a structure we have to use the keyword struct. Following the keyword, we type the name you want to give to the structure (this is also known as tag name). Then open braces, followed y the different data types . Close braces and give the names of the structure variables. Actually this is the structure definition.

Let me explain a little about 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 I want to reinforce is that, every bill will have a number and a particular amount mentioned in it.

Now suppose you want to keep a record of all the bills you have issued. You make use of your computer. What you have to store in your computer will be : 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 have the two data types). It is here that the concept of structures is useful.

Example :

struct bill
{
int billnumber;
float amount;
}
one,two;

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

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 billnumber and amount. Therefore, you can give the bill number and amount for bill one. Similarly, you can also give the details for bill 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.

INSTEAD OF THIS :
struct bill
{
int billnumber;
float amount;
}
one,two;

WE CAN WRITE IT AS FOLLOWS :

struct bill
{
int billnumber;
float amount;
};                                 // END OF STRUCTURE DECLARATION -  MEMORY HASN’T BEEN ALLOCATED            // SINCE THE STRUCTURE VARIABLES HAVEN’T BEEN DEFINED.
int main ( )
{
bill one, two;                       // STRUCTURE VARIABLE DEFINITION IS DONE IN THE MAIN FUNCTION
}

Both the methods are equivalent and are acceptable.

ACCESSING  and INITIALIZING STRUCTURE ELEMENTS :

To access a particular structure element we make use of the dot operator.

Syntax :

Structure variable                   .           structure element

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 :
one.billnumber = 214;
one.amount = 1032;
Bill one has a bill  number 214 and the amount of the bill is 1032.
One structure variable can be assigned to another only when they are of the same structure type.
two = one ;

Always remember that the quantity to the right of the equal to sign is assigned to quantity on the left.

In our example, one and two belong to the same structure bill. Hence the above statement is correct and it assigns the values of one.billnumber and one.amount to two.billnumber and two.amount.  

Our next section should make you clear with the concept of structures : 
An example using structures  


Or Go back to : Contents

Copyright © 2002 Sethu Subramanian All rights reserved.