Structure in C Language

Kailash Chandra Behera | Sunday, November 29, 2020

Introduction

C support constructed data structure. A structure is a collection of dissimilar data types under one name. Here in this article, we are going to discuss the structure in c language.

Getting started

Structure definition in C:- A struct in the C programming language (and many derivatives) is a composite data type (or record) declaration that defines a physically grouped list of variables under one name in a block of memory, allowing the different variables to be accessed via a single pointer or by the struct declared name which returns the same address.

The struct data type can contain other data types so is used for mixed-data-type records such as a hard-drive directory entry (file length, name, extension, physical address, etc.), or other mixed-type records (name, address, telephone, balance, etc.).

The C struct directly references a contiguous block of physical memory, usually delimited (sized) by word-length boundaries. It corresponds to the similarly named feature available in some assemblers for Intel processors. Being a block of contiguous memory, each field within a struct is located at a certain fixed offset from the start.

A structure declaration forms a template that may be used to create an instance of a structure.

The structure in c example:- let’s say you have a set of attributes of a student such as a roll no, name, age, and fees. The structure of the student will like the below code.

Above is the structure of the c programming language where “struct” is a keyword used to declare the structure variable. The “student” is the name of the structure called structure tag, which is used to give the name of the variable.

The roll no, name, age, and fees are called the structure member.

Important points of structure

  1. Structure members are variable of structure

  2. Each member may belong to a different data type.

  3. Once the declaration of structure, any number of instances of structure can be created.

  4. The member of the structure does not occupy any memory until they are associated with the structure variable.

  5. The member name of a structure must be unique with the structure but the same member name can be used in other structures.

  6. The link between member and structure variable is established using dot(.) or (->) operator. Example s1. Roll, s1->roll.

  7. There two ways to give value to the members that are

    1. Using an assignment statement

    2. Taking input from the keyboard.

  8. Without a tag name, the structure can be declared in the following way.

Programming struct:-

The following code writes a program using c to read roll no, name, and marks of 5 subjects of students of a class and find the average of marks secured by students in each subject.

 #include <string.h>  
 struct student{  
      int rollno;  
      char name[30];  
      int mark1, mark2,mark3,mark4,mark5;  
 }s[100];  
 void main(){  
      int i,n;float avg;  
      charscr();  
      printf("\n Enter the numer of students:");  
      scanf("%d",&n);  
      printf("\n Enter the roll number, name and marks of five students:");  
      for(i=0;<n;++){  
           scanf("%d%s%d%d%d%d%d",&s[i].rollno,s[i].name,&s[i].name,& s[i].mark1,& s[i].mark2, & s[i].mark3, & s[i].mark4, & s[i].mark5);  
      }  
      for(i=0;<n;++){  
           avg=(s[i].name+s[i].name+s[i].mark1+s[i].mark2+s[i].mark3+s[i].mark4+s[i].mark5);  
           printf("\n Average of %d student=%f", i, avg);  
      }  
      getch();  
 }  

Structure in c example

Difference between Structure and Union

Both structures and unions are user-defined data types. But the difference is that union allocates memory equals to the maximum size data member defined in it. Rest smaller size data members reuses the same memory space whereas structure allocates the total sum of individual data members size.

Example
 union student{  
      int rollno;  
      char name[30];  
      char branch[25];  
 }  

union example

So, the size of the union is 30 Bytes i.e, the size of the biggest data member. Whereas, if it is defined as a structure.

 struct student{  
      int rollno;  
      char name[30];  
      char branch[25];  
 }  

So when you declare a structure with same field then the size of the structure is 57 Bytes i.e, the sum of individual data members size.

Union allocates the biggest data members size, so the rest smaller data members reuse the same memory space. So whatever is the last updated value, it will be transferred throughout the program.

Unions are used for machine-independent codes because the compiler keeps track of the actual sizes of the variables. It can also be used for linked list programs.

Summary

In the above discussion, we learned what is structure in c with code example and how it is different from union. I hope you have enjoyed it a lot.

Thanks


No comments: