In one of our previous post we dealt with Array.Here in this post we'll be dealing with structure which I prefer to call 'The advanced version of arrays'.
Structure
Similar to an array, A structure is a collection, however of a dissimilar data types.
Take an example of an office, Where the Manager wants to keep the records of his/her employees.The record may include employees' name, address, department, pay scale , age and so on.So, why not create a variable with all those features in it. So when we want an entity with multiple features in it we can go with structures.
First Glance of Structure
struct student
{
int rollNo,intakeYear;
char name[20],address[20];
};
Above is a structure student with some of the features of the student like:Roll number, intake year, their name and address.
The keyword struct is used to define a structure. Structure definition always ends with a semicolon.
The general form of Structure definition is
struct <structure Name>
{
structure elements...
};
Since we have defined a structure,we can have a variable of student type. Let's See How!!
Variable Declaration for Structure
For the above structure student we can declare a variable of its type as:
struct student s1;
After the above line of code, the compiler allocates 20 bytes of memory for name , another 20 bytes for address, 4 bytes for roll number and another 4 bytes for intake year.So a total of 48 bytes of memory is allocated for the variable s1 .
However the following declaration of variable is also valid.
struct student
{
int rollNo,intakeYear;
char name[20],address[20];
}s1;
We can have as many variables we want.
Initialization of Structure Variable
Structure variable can be initialized when they are declared as:
struct student
{
int rollNo,intakeYear;
char name[20],address[20];
};
struct student s1 = {1,2020,"Raju","Kathmandu"};
struct student s2 = {0};
The second variable declaration initializes all its member to zero.
Accessing Structure Elements
We use dot operator(.) to access the structure element.
s1.rollNo;
This refers to the rollNo of the student.
It should be noted that before the dot operator(.) there should always be a structure variable and after the operator there must always be a structure element.
Array of Structure
What if we want to store the data of 100 students?we would require to use 100 different structure variable,which won't be a convenient way. A better approach will be to use an array of structure.
struct student
{
int rollNo,intakeYear;
char name[20],address[20];
}s1[100];
Above line of code declare a variable s1 for 100 student.This provides space in memory for 100 structures of the type struct student.
It should be appreciated what careful thoughts Dennis Ritchie has put into C language.He first defined array as a collection of similar elements then realized dissimilar data types that are often found in real life which cannot be handled using arrays ,so created a new data type called structure.But even using structures programming convenience could not be achieved,because a lot of variables were needed to be handled.Therefore he allowed us to create array of structures:an array of similar data types which themselves are a collection of dissimilar data types.Hats off to the Genius.
------------------Yashavant Kanetkar in his book (Let us C)
Since we have learnt some of the basics of structure,why not implement all of them in a program.
Write a Program to input name,faculty and GPA of 5 students and display the information of those student with faculty Engineering.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//structure declration
struct student
{
char name[20],faculty[20];
float GPA;
};
int main()
{
struct student s[5]
for(int i = 0 ; i < 5 ; i++)
{
printf("Enter name\n");
scanf(" %[^\n]",s[i].name);
printf("Enter faculty\n");
scanf(" %[^\n]",s[i].faculty);
printf("Enter GPA\n");
scanf("%f",&s[i].GPA);
while(s[i].GPA > 4.0)
{
printf("GPA cannot be greater than 4.0\n");
printf("Enter GPA\n");
scanf("%f",&s[i].GPA);
}
}
for(int i = 0 ; i < 5 ;i++)
{
if((strcmp(s[i].faculty,"Engineering")==0))
printf("Name:%s\n",s1[i].name);
printf("Faculty:%s\n",s1[i].faculty);
printf("GPA:%.2f\n",s1[i].GPA);
}
return 0;
}
Here we have a structure student
struct student
{
char name[20],faculty[20];
float GPA;
};
with structure elements name,faculty and GPA.
struct student s[5]
Here we declare an array of structure (for 5 students). We use a simple for loop to input the data to our structure variable.
s[i].name
Here we use a dot operator (.) to access the structure element.
Check your Understanding
1.Create a structure book,with structure elements :Book Name , Authors name ,total page , Price .And create array of structure for storing the data for 300 Books and display the information of book with price higher than 1000.
Well written brother 🔥
ReplyDeleteREALLY! Hats off to the genius of Dennis Ritchie😍
ReplyDelete