Structure in C – Set 3

In the last two instructional exercises, I gave a concise diagram and elements of structures in C programming. Structure is one of the most significant subjects of C programming.

The idea of structure is re-altered to make the item situated language C++. Today I will inform you regarding the different development highlights of structures in C programming.

I will likewise enlighten you concerning the employments of structures toward the finish of this instructional exercise. So let’s begin.

Structure in C

C language enabled the client to make your own factors. Structures are commonly used to make client characterized information types.

To make the use of structures like different factors, the components of structures are put away in bordering memory areas.

Because of this, one can without much of a stretch dole out every one of the estimations of one structure variable to another structure variable.

Recall that we can likewise piece supper duplicate by replicating each component of structure individually. Let’s make one program to get 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

Ashu 34 53.000000 
Ashu 34 53.000000 
Ashu 34 53.000000 
Press any key to continue...

Nesting is one fundamental element in C which gives the adaptability to make complex projects. Structures can likewise be settled to make some perplexing structures.

By and large software engineers like to remain inside 3 layers of settling. In any case, recall for all intents and purposes there is no restriction of settling in C. Consider the beneath program to get 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

Griag Ema John 23 
Press any hey to continue... 

Clarification

Settling of the structure is very basic. Consider cautiously the two speck administrators I have utilized inside print capacity to get to the components of the first structure.

#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

Shawn 32 
Shawn 32 
Press any hey to continue...

So far I informed you regarding the pointer to number, pointer to character, pointer to cluster, etc.

Like this, we can likewise make pointer to a structure. It is utilized to store the location of a structure. Recall that we can’t utilize (.) spot administrator to get to the structure components utilizing structure pointer.

We need to utilize bolt administrator (- >) for that reason. Consider the beneath code.

Clarification

Consider cautiously I have utilized bolt administrator to get to components at that address

Employments of Structure in C

Structures are commonly used to keep up databases in some association. One separate idea of Data Structures is additionally made to keep up the databases utilizing structures. Aside from this, some regular users are given underneath.

a) Formatting a floppy

b) Receiving a key from the console

c) Moving the cursor on the presentation

d) Making little games like Tic Tac Toe

e) Sending the yield to printer

Leave a Comment

error: Alert: Content is protected!!