In real life we need to have different data types. For example, to maintain employee information we should have information such as name age,qualification ,salary etc.
Here ,to maintain the information of employees dissimilar data types are required .Name and qualification ,salary is float.
A structure is a collection of one or more variables of different data types ,grouped together under a single name.by using structures we can make a group of variables ,array,pointers etc.
Declaration And Initialization of Structure :
Struct struct_type{
Type variable1:
Type variable2:
};
Pointer to structure ::
We know that a pointer is a variable that holds the address of other data variables . the variables may be of any data types i.e int float or double .In the same way we can also define pointers to structure . here the starting address of the member variables can be accessed .thus,such pointers to structure.pointers.
Example:
Struct book{
Char name[10];
Char author[25];
Int pages;
};
Struct book *ptr;
Note: *ptr is a pointer to a structure book.
Structure and Function ::
Like variables of standard data type structure variables also can be passed to the function by value or address.
Syntax:
Struct book{
Char name[35];
Char author[35];
Int pages ;
B1;
}
Void main(){
Statement:
show(&b1);
}
show(struct book *b2)
{
Statement:
}
Typedef:
By using typedef we can create a new data type .the statement typedef is to be used while defining the new data type .
Syntax:
Typedef type dataname;
Ex: typedef int hours;
NOTE:
ENUMERATED DATA TYPE :
The enum is a keyword .It is used for declaring enumeration type .the programmers can create his \her own data type and define what values the variables of these data types can hold.
Ex: enum day {Monday.tuesday,wednesday,thursday,friday,saturday,sunday};
UNION::
Union is a variable ,which is similar to the structure .It contains a number of members like a structure but it holds only one object. At a time.
Write a program to display size of structure elements .use sizeof() of operator.
#include<stdio.h>
#include<conio.h>
void main (){
struct book1{
char book[30];
int pages;
float price
};
struct book1 bk1;
printf("\n size of structure Elements :");
printf("\n Book : %d" ,sizeof(bk1.book));
printf("\n pages : %d" ,sizeof(bk1.pages));
printf("\n price : %d" ,sizeof(bk1.price));
printf("\n Total Bytes : %d" ,sizeof(bk1));
}
Output :
size of structure Elements :
Book : 30
pages : 4
price : 4
Total Bytes : 40
Write a program to declare pointer as members of structure and display the contents of the structure.
#include<stdio.h>
void main (){
struct sn{
char *name;
int *age;
float *height;
};
static struct sn *gn;
char nm[10] = "SANIKA";
int ag = 20;
float ht = 5.40;
gn->name = nm;
gn->age = &ag;
gn->height = &ht;
printf("\n Name = %s ",gn->name);
printf("\n age = %d ",gn->age);
printf("\n Height = %f ",gn->height);
}
Output :
Name = SANIKA
Age = 20
Height = 5.4
Write a program to create user-defined data type hours on int data type and use it in the program
#include<stdio.h>
#define H 60
void main (){
typedef int hours;
hours hrs;
printf(" Enter hours ");
scanf("%d ",&hrs);
printf("\nminuits = %d",hrs *H);
printf("\nsecond = %d",hrs *H*H);
}
Output:
Enter hours 2
Minutes = 120
Second = 7200
Write a program create a structure / class (name,address,mobile_no,marks);
#include<stdio.h>
struct student {
char name[20];
char address[50];
long int mobile_no;
float marks;
};
int main (){
struct student s;
printf(" Enter the name \n ");
gets(s.name);
printf("Enter the address\n ");
gets(s.address);
printf("Enter the mobile_no\n");
scanf("%ld",&s.mobile_no);
printf("Enter the marks\n");
scanf("%f",&s.marks);
printf("name : %s\n ",s.name);
printf("address : %s\n ",s.address);
printf("mobile_no : %ld\n ",s.mobile_no);
printf("marks : %2f\n ",s.marks);
return 0;
}
Output :
Enter the name
SANIKA
Enter the address
NANDED
Enter the mobile_no
91
Enter the marks
100
name : SANIKA
address : NANDED
mobile_no : 91
marks : 100.000000