Tuesday, 19 September 2017

Structures In C

Structures In C

A structure is an user defined data type. A structure contains a number of data types grouped together. These data types may or may not be of the same type. Let's take an example to understand the use of this data type.

void main( )
{
 struct book
 {
 char name ;
 float price ;
 int pages ;
 } ;
 struct book b1, b2, b3 ;
 printf ( "\nEnter names, prices & no. of pages of 3 books\n" ) ;
 scanf ( "%c %f %d", &b1.name, &b1.price, &b1.pages ) ;
 scanf ( "%c %f %d", &b2.name, &b2.price, &b2.pages ) ;
 scanf ( "%c %f %d", &b3.name, &b3.price, &b3.pages ) ;
 printf ( "\nAnd this is what you entered" ) ;
 printf ( "\n%c %f %d", b1.name, b1.price, b1.pages ) ;
 printf ( "\n%c %f %d", b2.name, b2.price, b2.pages ) ;
 printf ( "\n%c %f %d", b3.name, b3.price, 
 b3.pages ) ;
}

OUTPUT:

Enter names, prices and no. of pages of 3 books
A 100.00 354
C 256.50 682
F 233.70 512 

And this is what you entered
A 100.000000 354
C 256.500000 682
F 233.700000 512 

Declaring a Structure


In our example, the following statement declares the structure type:

struct book
{
 char name ;
 float price ;
 int pages ;
} ;

This statement defines a new data type called struct book. Each variable of this data type will consist of a character variable called name, a float variable called price and an integer variable called pages. The general form of a structure declaration statement is given below:

struct <structure name>
{
 structure element 1 ;
 structure element 2 ;
 structure element 3 ;
 ......
 ...... 



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, as,

struct book b1, b2, b3 ; 

Accessing Structure Elements 

Let us see how the elements of the structure can be accessed. Structures use a different scheme. They use a dot (.) operator. So to refer to pages of the structure defined in our sample program we have to use,

b1.pages

Similarly, to refer to price we would use,


b1.price

Features Of Structures:

  • The values of a structure variable can be assigned to another structure variable of the same type using the assignment operator. 
  • One structure can be nested within another structure. Using this facility complex data types can be created. The following program shows nested structures:

                    void main( )
                    {
                       struct address
                      {
                         char phone[15] ;
                         char city[25] ;
                         int pin ;
                        } ;
                       struct emp
                       {
                          char name[25] ;
                          struct address a ;
                        } ;
                       struct emp e = { "jeru", "531046", "nagpur", 10 };
                       printf ( "\nname = %s phone = %s", e.name, e.a.phone ) ;
                        printf ( "\ncity = %s pin = %d", e.a.city, e.a.pin ) ;
                      }

                     OUTPUT:

                         name = ABC phone = 93314*****
                         city = PATNA pin =  800045


  • 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
  • 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 struct. Such pointers are known as ‘structure pointers’.

No comments:

Post a Comment