08_structure
08_structure
STRUCTURE IN C LANGUAGE
-------------------------------------------------------------------------
};
Example:
Declaration of structure
Struct student
{
Char name[20];
int rollno;
float marks;
};
int main()
{
//initialization of structure
strcpy(s1.name,”david”);
s1.rollno = 210;
s1.marks = 77.5;
}
Taking input from user or keyboard for details into structure.
Example:
Declaration of structure
Struct student
{
Char name[20];
int rollno;
float marks;
};
int main()
{
//Taking input from user
If users wants to enter more than one details of student than what should
we do?
First method:
Example:
Declaration of structure
Struct student
{
Char name[20];
int rollno;
float marks;
};
int main()
{
//Taking input from user
Example:
Declaration of structure
Struct student
{
Char name[20];
int rollno;
float marks;
};
int main()
{
//Taking input from user
-------------------------------------------------------------------------
Nested structure in c language
-------------------------------------------------------------------------
Syntax:
struct <tag_name>
{
data_member;
.....
struct <tag_name>
{
data_member;
....
...
};
};
Example:
struct manager
{
char mgr_name[20];
int mgr_id;
float mgr_salary;
struct employee
{
int emp_id;
char emp_name[20];
float emp_salary;
}emp;
}mgr;
int main()
{
clrscr();
printf("Enter manager name : ");
scanf("%s",mgr.mgr_name);
printf("Enter manager id : ");
scanf("%d",&mgr.mgr_id);
printf("Enter manager salary:");
scanf("%f",&mgr.mgr_salary);
-------------------------------------------------------------------------
STRUCTURE PASSING FUNCTION
-------------------------------------------------------------------------
Syntax:
struct <tag_name>
{
data_member;
.....
};
return_type main_fun(void)
{
Statement;
.....
....
}
return_type func_name(parameter_list) {
data_member;
......
.....
}
Example:
struct product
{
char pro_name[20];
int pro_id;
float pro_price;
}pro;
void main()
{
clrscr();
printf("Enter product name : ");
scanf("%s",pro.pro_name);
printf("Enter product id : ");
scanf("%d",&pro.pro_id);
printf("Enter product price : ");
scanf("%f",&pro.pro_price);
getch();
}
void showData(struct product p1)
{
printf("product name is : %s\n",p1.pro_name);
printf("product id is : %d\n",p1.pro_id);
printf("product price is : %f\n",p1.pro_price);