Structures in C Programming language

Definition Structure – Structure is classified as one of the structured data types. It is used to group the same type of data items that belong to different data types . As we have seen that arrays can be used to represent a group of data items that belong to the same type. Therefore we cannot use an array if we want to represent a collection of data items of different types using a single name. Fortunately C pr0gramming supports a data type known as structure to overcome the limitation of the array. Ex – it  can be used to represent a set of attributes, such as student_name, roll_number and marks. It is analogous to that of a record in many other languages.

Declaring Structure and it’s variables –

Structure data type is declared using the keyword ‘ struct ‘ . The variables declared in the structure are called structure elements or members. Each member may belong to a different type of data. Also a structure member declaration is similar to the declaration of variables of any other data type. Declaration of structure  includes the following elements

  • The keyword struct.
  • The structure tag name.
  • List of variable names separated by commas.
  • A termination semicolon.

Following is the syntax to define a structure and it’s members –

struct   tag_name
{
     data_type member1;
     data_type member2;
     ---   ---
     ---   ---
};

After defining the structure, declare the structure as follow –

struct tag_name variable_1, variable_2;

You can also define and declare the structure at the one place only. Also remember that the members of the structure themselves are not variables. They don’t occupy any memory until they are associated with the structure variable.
Example of declaring a structure –

struct book_bank
{
   char title[20];
   char author[15];
   int pages;
   float price;
};
struct book_bank book1, book2, book3;

Here book1, book2 and book3 are the structure variables representing three books. Each structure variable will contain the title, author, pages and price data items respectively.

Accessing Structure Members

As mentioned earlier, the members of the structure themselves are not variables. So we cannot access these members as we access the variables. In C programming language to access the members of the structure, they should be linked to the structure variables. To do this (.) dot operator also known as member operator, is used for linking the members with the structure variable. Syntax for this is as follows –

structure_variable.member_name

For ex -
book1.price
book1.pages

These are the variables representing the price and page of book1. and this full variable now can be treated like any other ordinary variable.

Structure Initialization

As any other data type structure can be initialized in the following way in the C programming language –

struct st_record
{
   int weight;
   float height;
}   student1 = { 60, 180.75 }

In the above example, structure is initialized in the two places. First structure variable is initialized outside the main function and tag name is not used for initializing the structure variable. This variable assigns the value 60 to student.weight and 180.75 to student.height. This is a one to one correspondence between the members and their initializing values.Second structure variable is initialized inside the main function and for this the tag name is required.

C programming language does not permit the initialization of individual structure members within the template. the initialization must be done only in the declaration of the actual variables.

Rules for Initialization structure

There are few rules to keep in mind while initializing structure variables at compile-time.

  1. We cannot initialize individual members inside the structure template.
  2. The order of values enclosed in braces must match the order of members in the structure definition.
  3. It is permitted to have a partial initialization. We can initialize only the first few members and leave the remaining blank. The uninitialized members should be only at the end of the list.
  4. The uninitialized members will be assigned default values, i.e – Zero for integer and floating point numbers and  ‘\0’ for characters and strings

Sample Structure program

Below is the code of the sample structure program to know how to use structures in the C language –

/* Sample structure program in C language */
#include<stdio.h>
#include<conio.h>

struct book_bank
{
       char title[20];
       char author[15];
       int pages;
       float price;
       };              /* Structure Definition */
       
void main()
{
     struct book_bank book1;  /* structure Declaration */
     
     printf(" Input title, author, pages and price of the book \n");
     gets(book1.title); 
     gets(book1.author);
     scanf(" %d %f",&book1.pages,&book1.price); 
         
     puts(book1.title);
     puts(book1.author);      /* Accessing strcuture variable*/
     printf(" %d \n%f",book1.pages,book1.price);  
               
     getch();
}

Output – C programming
John
150
100.99

yashan has written 70 articles

3 thoughts on “Structures in C Programming language

Cancel reply

Leave a Reply to Govind Singh

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>