C Language Structures

A structure in “C” Language is a collection of the heterogeneous data type. Where you can store the value of different types in form of a grouped list in a single variable. The value of structure accessed using struct declared name or through the pointer. Structures are defined using Struct keyword.

The syntax of Structures:

Struct <Structute Name> {
Datatype     <Variable1>;
Datatype     <Variable2>;
Datatype     <Variable3>;
}<structure variables optional>;

Suppose you want to store information about some people. In such case, the arrays are not useful because personal information data is never homogeneous. For example

NAME : Mr. SUBHRA LAHIRI
AGE     : 20
Marks : 85.77
CLASS : 3rd Year(Computer Sc.)

Shows that “Name” is String of character type, whereas “Age” and “Marks” are number type. In order to handle such situations. C Language provides a facility known as structures.

We know that Arrays provide the facility of grouping homogeneous data items into a single object while structure group together heterogeneous data types into a single object. To store the above information we can define a structure as follows

struct information{
     char name[40];
     int age;
     float marks;
     char class[30];
};

or

struct infomation{
     char name[40];
     int age;
     float marks;
     char class[30];
}info1,info2;

we can define structure variable at the end of the structure it can be one or more than one as per our requirement. we can also define structure variable like that:

// Include File//


struct infomation{
     char name[40];
     int age;
     float marks;
     char class[30];
};

void main()
{
   struct information info1; /* Declare info1 of type information */
   struct information info2; /* Declare info2 of type information */

   /* use info1 variable to perform here 
     .....
     .....
   */


}

We can also define an array of structure. The array of structure is useful and has another importance when you think a structure in a tabular form or format. Its gives you ability to store the pieces of information as like spreadsheet and database. Let’s see the example of an array of structure.

#include<stdio.h>


struct infomation{
     char name[40];
     int age;
     float marks;
     char class[30];
};

void main()
{
   struct information info[5]; /* Declare an array info of 5 row of type information */
  
   


}

Struct information info[5] which defines an array with five elements, each of which is of type struct information, i.e. each is an information structure. We can imagine this as:

structures

This is very similar to a two-dimensional array, except that in an array, all data items must be of the same type, where an array of structures consists of columns, each of which may be of a distinct data type. Here one question arises i.e, is an array of structure violate the rule of the array that is homogeneous data type? The answer is No. It does not violate the rule of the array. A structure itself a data type. When we declare an array of structure, this is homogeneous data type of that structure. That’s why the array of structure not violating the rule of an array.

Storing and retrieving value from a structure:

#include <stdio.h>
#include <string.h>
 
struct infomation{
     char name[40];
     int age;
     float marks;
     char class[30];
};
 
int main( ) {

   struct information info1;        /* Declare info1 of type information */
   struct information info1;        /* Declare info2 of type information */
 
   /* info 1 specification */
   strcpy( info1.name, "Subhra Lahiri");
   info1.age=20;  // (.) dot operator is use to access structure element // 
   info1.marks=85.77; 
   strcpy( info1.class, "3rd year");
  

   /* info 2 specification */
   strcpy( info2.name, "Bivas Mondal");
   info2.age=19;
   info2.marks=81.77; 
   strcpy( info2.class, "2nd year");
 
   /* print info1 data */
   printf( "info 1 name : %s\n", info1.name);
   printf( "info 1 Age  : %d\n", info1.age);
   printf( "info 1 Marks: %f\n", info1.marks);
   printf( "info 1 Class: %s\n", info1.class);

   /* print info2 data */
   printf( "info 2 name : %s\n", info2.name);
   printf( "info 2 Age  : %d\n", info2.age);
   printf( "info 2 Marks: %f\n", info2.marks);
   printf( "info 2 Class: %s\n", info2.class);

   return 0;
}

Pointers to Structures:

Define pointers to structure similar to define a pointer of other variables. We can store the address of structure variable using & operator.

#include <stdio.h>
#include <string.h>
 
struct information{
     char name[40];
     int age;
     float marks;
     char class[30];
};

void viewinfo( struct information *info ); // prototype of viewinfo
 
int main( ) {

   struct information info1;        /* Declare info1 of type information */
   struct information info2;        /* Declare info2 of type information */
 
   /* info 1 specification */
   strcpy( info1.name, "Subhra Lahiri");
   info1.age=20;  // (.) dot operator is use to access structure element // 
   info1.marks=85.77; 
   strcpy( info1.class, "3rd year");
  

   /* info 2 specification */
   strcpy( info2.name, "Bivas Mondal");
   info2.age=19;
   info2.marks=81.77; 
   strcpy( info2.class, "2nd year");


   //print info1 data by passing address of info1//
   viewinfo(&info1);
   printf("\n");
   //print info2 data by passing address of info2 //
   viewinfo(&info2);

   getch();
 
   
   return 0;
}

void viewinfo(struct information *info){
   printf( "Name : %s\n", info->name);
   printf( "Age  : %d\n", info->age);
   printf( "Marks: %f\n", info->marks);
   printf( "Class: %s\n", info->class);
}

Output:

output2

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.