Structure in C – Set 2

In the last instructional exercise, I educated you regarding the essential utilization of structure in C programming. I gave an outline of affirmation and the meaning of structures.

Furnished with that fundamental learning, today I will enlighten you regarding how to access structure components.

Aside from this, I will likewise educate you regarding the memory portion of structure components and cluster of structure. So let’s begin.

Structure in C

Getting to Elements of Structure

It is critical to characterize the structure before getting to it. In exhibits, we utilize subscript to get to the cluster components.

Be that as it may, this idea is marginally not quite the same as them. To get to the structural components we use dab administrator or part get to the administrator (.).

//structure definition
struct student
{
 char name[10];
 int roll;
 int marks;
}s1;
 
struct student s1={"Rishi",4,56}; //storing values
 
//accessing structure elements
printf("%s%d%d",s1.name,s1.roll,s1.marks);

We need to compose the name of the structure variable pursued by a spot and structure component. One little model is given beneath.

Above code will show the name, move number and signs of understudy s1. Consider cautiously I have gotten to the components utilizing speck administrator.

Memory Allocation of Structure Elements

Structure components are put away like cluster components. It implies the structure components are likewise put away in bordering memory areas.

As I have said before, at the hour of structure variable presentation, memory is assigned to it. In our above model, 14 bytes (10 for string and 4 for two numbers) will be apportioned for s1.

Array of Structure

Before continuing to this theme I need to ask you – Is it conceivable to make an exhibit of structure? Well obviously Yes.

On the off chance that we can make an exhibit of pointers, a cluster of burns, etc then we can likewise make a cluster of structure.

#include<stdio.h>
 
int main() 
{  
 struct library
 {
  char lang[10];
  int pages;
  char name[20];
 };
 
 struct library lib[3];
 int i;
 
 for(i=0;i<3;i++)  
 {   
  printf("nEnter language, pages and namen");   
  scanf("%s%d%s",&lib[i].lang,&lib[i].pages,&lib[i].name);  
 }  
 
 for(i=0;i<3;i++)     
  printf ( "n%st%dt%sn",lib[i].lang,lib[i].pages,lib[i].name); 
 
 return 0;
}

Output

Enter language, pages and name 
hindi 100 ankit 

Enter language, pages and name 
English 120 anmol

Enter language, pages and name 
maths 100 sunita 

hindi 100 ankit 

English 120 anmol 

maths 100 sunita 
Press any hey to continue... 

Making a variety of structure is like making an ordinary cluster. For this, we just need to characterize a structure. After that, we need to make a variety of structure. Consider underneath program to comprehend it appropriately.

Clarification

In this model, I am putting away the records of books in a library framework. You can see I have made a variety of structure of size 3 and that is putting away 3 records from the client and afterwards showing the equivalent on the screen.

Leave a Comment

error: Alert: Content is protected!!