home
C TUTORIALS

Structure

Structure contains a number of data types grouped together.These data types may or may not be of the same type.
There are two aspects of structures:
(a)declaration of a structure
(b)accessing of structure elements

Declaring a structure

The general form of a structure declaration statement is given below:
struct { structure element 1; structure element 2; structure element 3; ... ... };
Let us take and example of structure to better understand it:
struct book { char name; float price; int pages; }
This statement defines a new data type called struct book. Each of the variable of this data type will consists of a char variable called name, a float variable called price and an int variable called pages.
Once the new structure data type has been defined, one or more variables can be declared to be of that type.
For example, the variables b1,b2,b3 can be declared to be of the type struct book, a
struct book b1,b2,b3;
NOTE:1. Above statement sets aside space in memory.
2. These bytes are always adjacent memory locations.
3.We can combine the declaration of the structure type and the structure variables in one statement.
struct book { char name; float price; int pages; }; struct book b1,b2,b3;
is same as...
struct book { char name; float price; int pages; } b1,b2,b3;
4.Structure variables can also be initialized where they are declared.
struct book { char name; float price; int pages; }; struct book b1={"Basic",130.00,550}; struct book b1={"Physics",150.80,800}; struct book b1={0};
If a structure variable is initiated to value {0},then all its elements are set to value 0.

Accessing Structure Elements

Structure use a dot (.) operator.before the dot, there must always be a structure variable and after the dot, there must always be a structure element.

How Structure Elements are stored

Whatever be the elements of a structure, they are always stored in contiguous memory locations.

Array of structures

In our sample program above, to store data of 100 books, we would be required to use 100 different structure variables from b1 to b100, which is definitely not very convenient.A better approach would be to use an array of structures.
(a)how the array of structures is declared....
struct book b[100]
This provides space in memory for 100 structures of the type struct book.
(b)The syntax use for reference each element of the array b is similar to the syntax used for arrays of ints and chars.
For example:We refer to zeroth book's price as b[0].price.
(c)In an array of structure, all elements are always stored in adjacent memory locations.

Additional Features of Structures

(a)The value of a structure variable can be assigned to another structure variable of the same type using the assignment operator.(not necessary to copy piece-meal)
(b)One structure can be nested in other structure
for example:
struct address { char phone[15]; char city[25]; int pin; }; struct emp { char name[25]; struct address a; };
To access the element of a structure that is part i=fo another structure, dot operator is used twice, as in the expression,
e.a.pin or e.a.city
We can nest a structure within a structure within a structure, within another structure,and so on...
(c)Like an ordinary variable, a structure variable can also be passed to a function.WE may either pass individual structure elements or the entire structure variable at one shot
(d)The way we can have a pointer pointing to an int, or a pointer pointing to a char, similarly we can have a pointer pointing to a structure.
consider the previous example struct book b1; struct book *ptr ; //pointer to structure ptr=&b1;
C provides an operator ->, called an arrow operator to refer to the structure elements.