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:
structure-variables; // Structure definition
‘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:
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:
// End of struct declaration–memory not allocated
since struct variable not defined
int main ( )
bill one, two; //Structure variable definition
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
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.
one.billnumber = 214;
one.amount = 1032.25;
Bill ‘one’ has a bill number 214 and the ‘amount’ of the bill
One structure variable can be assigned to another only when they are of the same structure type.
two = one;
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:
//Declaring the structure phonebookstruct phonebook
The output would be:
Enter the name : Ajay
Enter the city : Chennai
Enter the telephone number : 21345
The size of the structure variable is : 84
The entry you made is (a1): Ajay-Chennai-21345
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:
//Another structure called ‘date’
date birthday; //birthday is a variable of the structure ‘date’
int main( )
The output of the program is:
Enter the name : Mathew
Enter the city : Delhi
Enter the telephone number : 65478
Enter the date of birth (day, month and year) :
2 12 1980
The size of the structure variable is : 96
The entry made is:Mathew-Delhi-65478
Birthday is on : 2-12-1980
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.
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:
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.