Why we use Structures In C ?
When we deal with real world data ,we don’t usually deal with only int,char,float.. Instead of this we deal with real world entities such as student,book,teacher.. etc which have their own attributes to define the behavior of entity .
For Ex Student have attributes like stud_roll,stud_id,stud_branch,stud_sec etc.C provides a data type called Structures to deal with real world entities .
Structures in C gather together ,different attributes of information of the real world entity .
Let us examine it by using a example:
We have to write a program to store the details of the books like name,price and pages for a book store.
Code:
#include<stdio.h>
int main()
{
struct book
{
char name;
char auth_name[30];
int pages;
float rate;
};
struct book a={‘a’,”buffercode.in”,500,1023.89};
struct book b,c;
b.name=a.name;
strcpy(b.auth_name,a.auth_name);// b.auth_name=a.auth_name is wrong in care of string
b.pages=a.pages;
b.rate=a.rate;
c=b;
printf(“%c %s %d %fn”,a.name,a.auth_name,a.pages,a.rate);
printf(“%c %s %d %fn”,a.name,a.auth_name,a.pages,a.rate);
printf(“%c %s %d %fn”,a.name,a.auth_name,a.pages,a.rate);
return 0;
}
Output:
a buffercode.in 500 1023.890015a buffercode.in 500 1023.890015
a buffercode.in 500 1023.890015
Now look at the concepts uses in the above program
Declaration of structure
//Following declare the structure in c
struct book
{
char name;
char auth_name[30];
int pages;
float rate;
};
In this statement we defines a new data type struct book same as we already have data type of int,char etc.
Now each variable(struct book a,b,c) of this data type(struct book) consist of 4 variables i.e., character variable called name,string var called auth_name,int variable called pages,float var called rate.
Declaring and defining the variables of data type Structure in C
The way to declare the variables in new data type called structure in c is same as the way of declaration of variables in other data types like int,char,float etc
- First way to declare the data type struct is by defining and declaring of variables at the same time.
For Ex: struct book a={‘a’,”buffercode.in”,500,1023.89}; - Second way to declare is as follows:
struct book b,c; - struct book
{
char name;
char auth_name[30];
int pages;
float rate;
}b,c;
Above declaration is the same as struct book b,c;
NOTE:
- In the above declaration of variables ,each variable holds the space of 37 bytes: 1 for name ,30 for author name,2 for pages and 4 for rate.
- If you define the variables at the same time you declare it, the closing braces must be followed by semicolon .
for ex: struct book a={‘a’,”buffercode.in”,500,1023.89}; - Structure type declaration doesn’t tell the compiler to reserve some space instead of this it’s variable do it.
- If a struct variable is set to a value {0} it’s all element are set to a value 0.
- Usually we declare the structures in c at the top of program but in very large programmes like in kernal programming we declare them in a separate header file and than that file is included using the preprocessor #include.
Accessing the elements of structure in c
Structures in c uses a dot(.) operator to access the elements .
For Ex:To access the author name we have to use
a.author_name
Similarly to access the rate elements we have to use
b.rate or a.rate
where a and b are variables of books.