Structure in C – Part 3

Read: Structure in C – Part 2

In the last two tutorials I gave a brief overview and functions of structures in C programming. Structure is one of the most important topic of C programming. The concept of structure is re-modified to make the object oriented language C++. Today I will tell you about the various advance features of structures in C programming. I will also tell you about the uses of structures at the end of this tutorial. So lets get started.

Structure in C

1. C language gave user the power to create your own variables. Structures are generally used to create user defined data types. To make the functionality of structures similar to other variables, the elements of structures are stored in contiguous memory locations. Due to this, one can easily assign all the values of one structure variable to other structure variable.

Remember we can also do piece meal copy by copying every element of structure one by one. Lets make one program to understand this.

#include<stdio.h>
#include<string.h>
 
void main() 
{  
 struct member  
 {   
  char name[10];   
  int age;   
  float mem_no;  
 };  
 
 struct member a1={"Ashu",34,53};  
 struct member a2,a3;
 
 /* piece-meal copying */  
 strcpy(a2.name,a1.name);  
 a2.age=a1.age;
 a2.mem_no=a1.mem_no;  
 
 /* copying all elements at one go */  
 a3=a2;  
 
 printf("%s %d %f",a1.name,a1.age,a1.mem_no);  
 printf("n%s %d %f",a2.name,a2.age,a2.mem_no);
 printf("n%s %d %fn",a3.name,a3.age,a3.mem_no); 
}

Output

Structure in C - Part 3

2. Nesting is one main feature in C which gives the flexibility to create complex programs. Structures can also be nested to create some complex structures. Generally programmers prefer to stay within 3 layers of nesting. But remember practically there is no limit of nesting in C. Consider the below program to understand this.

#include<stdio.h>

void main() 
{ 
 struct stud
 { 
  char name[10];
  int age; 
 };

 struct parent_name
 {
  char fath_name[10];
  char moth_name[10];
  struct stud a1;
 };
 
 struct parent_name s1={"Griag","Ema","John",23};
 
 printf("%s %s %s %dn",s1.fath_name,s1.moth_name,s1.a1.name,s1.a1.age);
}

Output

Structure in C - Part 3

Explanation
Nesting of structure is quite simple. Consider carefully the two dot operators I have used inside printf function to access the elements of first structure.

3. So far I told you about the pointer to integer, pointer to character, pointer to array and so on. Similar to this we can also make pointer to a structure. It is used to store the address of a structure. Remember we cannot use (.) dot operator to access the structure elements using structure pointer. We have to use arrow operator (->) for that purpose. Consider the below code.

#include<stdio.h>

void main() 
{ 
 struct stud 
 { 
  char name[10];
  int age; 
 };
 
 struct stud s1={"Shawn",32};
 struct stud *s2;
 s2=&s1; 
 
 printf("%s %d",s1.name,s1.age);
 printf("n%s %dn",s2->name,s2->age);
}

Output

Structure in C - Part 3

Explanation
Consider carefully I have used arrow operator to access elements at that address

Uses of Structure in C

Structures are generally used to maintain databases in some organisation. One separate concept of Data Structures is also made to maintain the databases using structures. Apart from this some very common uses are given below.

a) Formatting a floppy
b) Receiving a key from the keyboard
c) Moving the cursor on the display
d) Making small games like Tic Tac Toe
e) Sending the output to printer

Leave a Comment

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