Arrays ka use kisi group of data ko store karne ke liye hota hai jo same data type ke hote hai, like int or float. However, ham ek array ka use different data type ke items ko store karne ke liye nhi kar sakte. Fortunately, C ek constructed data type ko support karti hai jo ki structures hota hai. Ye ek mechanism hota hai jo ki different data types ke items ko store kar sakta hai. Structure ek convenient tool hota hai for handling a group of logically related data items. For example, ye use ho sakta hai for represent a set of attributes, jaise student_name, roll_number, and marks.
More examples of structures:
time : second, minutes, hours
date : day, month, year
book : author, title, price, year
city : name, country, population
address : name, door-number, street, city
Structure se ham complex data ko more meaningful way mein organize kar sakte hai. Ye ek powerful concept hota hai jo ham often use karte hai to design our program.
Defining A Structure
Structure ek way hota hai related variable ko group karne ka into a single unit. Pehle hame structure ka format define karna padta hai, aur baad mein us structure ko use karte hai. For example:
struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
};
The keyword struct declares karta hai ek structure jo four data fields ko hold karta hai, title, author, pages, and price. Ye fields structure elements or member kehlate hai. Har member ek different type ke data se belong karta hai. Yaha structure ka name book_bank hai aur ise structure tag kehte hai.
The general format of a structure definition:
struct tag_name
{
data_type member1;
data_type member2;
---- ----
---- ----
};
Note about structure defining syntex:
- Template ko semicolon se terminate karna.
- Entire definition ek statement consider hoti hai, lekin each member uske name aur type se seperately declare karna hota hai inside the template.
- The tag name jaise book_bank ka use program mein baad mein structure variable ko declare karne ke liye hoga.
Declaring Structure Variables
Structure ke format ko define karne ke baad ham is type ke variables ko declare kar sakte hai. Ek structure variable declaration similar hoti any data type variable declaration se.
- The keyword struct
- The structure tag name
- List of variable names separeted by commas.
- A terminating semicolon.
For Example, the statement
struct book_bank book1, book2, book3;
declares book1, book2, aur book3 as variables of type struct book_bank.
Har ek variables ke pass four members hai jo ki template mein specified hai. The complete declaration:
struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
};
struct book_bank book1, book2, book3;
Structure ke members khud mein koi variables nhi hai. Ye koi bhi memory occupy nhi karenge jabtak ye structure variable such as book1 se associate na ho. Ham structure definition aur variable declaration dono ko ek sath combine karke ek statement mein bhi likh sakte hai.
The declaration
struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
} book1, book2, book3;
Type-Defined Structures
Ham keyword typedef ka use bhi kar sakte hai to define a structure
typedef struct
{ . . . . . .
type member1;
type member2;
. . . .
. . . .
}type_name;
The type_name represents karta hai structure ki definition jo iske sath associated hai aur isliye iska use structure variables declare karne ke liye ho sakta hai.
type_name variable1, variable2, ......;
Remember that
- The type_name type definition name hai not a variable aur
- Ham ek variable ko define nhi kar sakte typedef declaration se.
Lets start code...
Program: Ek structure type define karenge, struct personal se jo ki person name, date of joining aur salary contain karega.Using this structure, ham ek program build karenge jo ek person ki information ko keyboard se input karege aur screen par print karega same.
#include <stdio.h>
struct personal
{
char name[20];
int day;
char month[10];
int year;
float salary;
};
int main(int argc, char const *argv[])
{
struct personal person;
printf("Input Values \n");
scanf("%s %d %s %d %f", person.name, &person.day, &person.month, &person.year, &person.salary);
printf("%s %d %s %d %f\n", person.name, person.day, person.month, person.year, person.salary);
return 0;
}
Output:
Input Values
Shakib 1 April 2005 35000
Shakib 1 April 2005 35000.000000
Explanation:
- First hamne ek personal structure define kara.
- Is structure mein 5 members hai name, day, month, year, aur salary. Har ek different data types se belong karta hai.
- main() function mein hamne ek structure variable declare kara. struct personal person;
- Than scanf() se all five values ko input kar liye, structure variable ki values ko input karne ke liye ham . (dot operator) ka use karte hai.
- End mein printf() se hamne input li hui values ko hi output kar diya again using . (dot operator).
Comments