Professional Documents
Culture Documents
Structure-defination:
Declaration of structure:
Syntax:
struct tag_name
{
data_type member_1;
data_type member_2;
data_type member_3;
|
|
|
|
data_type member_n;
};
Example1:
struct student
int no;
char name[10];
int marks;
float avg;
};
Example2:
struct book
{
char name[20];
char author[20];
int pages;
float price;
}b1,b2;
In the above example the b1.b2 are the structure variables that can be used to
create the structures in the memory.
Structure variable:
struct employee
int eno;
char ename[20];
float salary;
}e1,e2,e3;
In the above example e1,e2,e3 are the structure variables.e1 is the structure
variable that containing e1{eno,ename,salary},e2{eno,ename,salary} and
e3{eno,ename,salary} where the memory is allocated for these three variables
as follows.
For e1:
For e2:
For e3:
Like the above the e1,e2,e3 are taken as the referential objects to the structure
employee.
Intialization1:
struct employee
int eno;
char ename[20];
float salary;
}e1,e2;
main()
e1={1,”Radha”,15000.00};
e2={2,”Padma”,20000.00};
-----------------
-------------------
In the above example the values are stored in the structures as follows
For e1:
1 Radha 15000.00
For e2:
2 Padma 20000.00
Example1:
Program:
#include<stdio.h>
#include<conio.h>
struct student
int sno;
char sname[20];
int marks;
float avg;
};
main()
clrscr();
printf("student information\n");
printf("sno=%d\n",s1.sno);
printf("sname=%s\n",s1.sname);
printf("marks=%d\n",s1.marks);
printf("avg=%f",s1.avg);
getch();
Output:
student information
sno=1
sname=revanth
marks=367
avg= 61.14
Output:
1.Array of structures
1.Arrays of structures:
struct student
int sno;
char sname[10];
float smarks;
}s[50];
Example:
Program:
struct student
char name[20];
int sub1,sub2,sub3,total;
float avg;
};
main()
int i;
clrscr();
for(i=0;i<=2;i++)
scanf("%s",s[i].name);
scanf("%d%d%d",&s[i].sub1,&s[i].sub2,&s[i].sub3);
s[i].total=s[i].sub1+s[i].sub2+s[i].sub3;
s[i].avg=s[i].total/3;
for(i=0;i<=2;i++)
printf("\n***************student
information****************”);
printf("name=%s\n",s[i].name);
printf("sub1=%d\tsub2=%d\tsub3=%d",s[i].sub1,s[i].sub2,s[i].sub3
);
printf("\ntotal=%d",s[i].total);
printf("\naverage=%f",s[i].avg);
getch();
Output:
narasimha
71
62
63
khan
84
62
90
dinesh
73
52
91
**************student information*******************
name=narasimha
total=196
average=65.000000
**************student information*******************
name=khan
total=236
average=78.000000
**************student information*******************
name=dinesh
total=216
average=72.000000
National Junior college Programming in ‘C’ Page 9
Jr CSE Unit-V Structures
Example:
struct student
int sno,total;
char name[10];
int marks[6],avg;
}s1;
Example:
Program:
#include<stdio.h>
struct cse
char sname[20];
int sub[6];
int total;
float avg;
};
main()
struct cse s;
int i;
s.total=0;
clrscr();
scanf("%s",s.sname);
for(i=0;i<=5;i++)
scanf("%d",&s.sub[i]);
for(i=0;i<=5;i++)
s.total=s.total+s.sub[i];
s.avg=s.total/6;
printf("\nstudent details\n");
printf("name=%s\n",s.sname);
printf("subject marks\n");
for(i=0;i<=5;i++)
printf("%d\n",s.sub[i]);
printf("total=%d\naverage=%f\n",s.total,s.avg);
getch();
Output:
National Junior college Programming in ‘C’ Page 11
Jr CSE Unit-V Structures
joel
74 71 63 68 71 63
student details
name=joel
subject marks
74 71 63 68 71 63
total=410
average=68.000000
Both the structure variable and the variables in the structures containg arrays
then it became under this type.
Example:
struct employee
int id;
char ename[20];
float esal;
}e[10];
Nested structutes:
Example:
struct student
int rollno;
char sname[20];
struct dob
int d,m,y;
};
};
Example:
struct student
int rollno;
char sname[20];
struct date
int d,m,y;
}dob,doj;
};
main()
{
National Junior college Programming in ‘C’ Page 13
Jr CSE Unit-V Structures
struct student s;
clrscr();
scanf("%d",&s.rollno);
scanf("%s",s.sname);
scanf("%d%d%d",&s.dob.d,&s.dob.m,&s.dob.y);
scanf("%d%d%d",&s.doj.d,&s.doj.m,&s.doj.y);
printf("---------student details---------\n");
printf("rollno=%d\n",s.rollno);
printf("student name=%s\n",s.sname);
printf("date of birth=%d-%d-%d\n",s.dob.d,s.dob.m,s.dob.y);
printf("date of joining=%d-%d-%d",s.doj.d,s.doj.m,s.doj.y);
getch();
Output:
101
raja
10 9 1991
National Junior college Programming in ‘C’ Page 14
Jr CSE Unit-V Structures
26 9 2008
---------student details---------
rollno=101
student name=raja
date of birth=10-9-1991
date of joining=26-9-2008
Unions:
Syntax:
union tag_name
{
data_type member_1;
data_type member_2;
data_type member_3;
|
|
|
|
data_type member_n;
}unionvariable;
Example:
National Junior college Programming in ‘C’ Page 15
Jr CSE Unit-V Structures
union employee
int id;
char name[10];
float salary;
}e1,e2;
In the above example the union allocates the memory for the highest data
element i.e float allocating 4 bytes so it allocates memory onl 4 bytes and share
it for all variables.
Example:
Program:
union employee
int id;
char name[10];
Float salary;
} e1;
main ()
clrscr();
scanf("%d",&e1.id);
scanf("%s",e1.name);
scanf("%f",&e1.salary);
printf("employee details\n");
printf("id=%d\n",e1.id);
printf("name=%s\n",e1.name);
printf("salary=%f",e1.salary);
getch();
Output:
Enter employee id
101
sumanth
10000
Employee details
Id=16384
Name=
Salary=10000.000000
National Junior college Programming in ‘C’ Page 17
Jr CSE Unit-V Structures
STRUCTURE UNION
1.The syntax of structure is as follows. 1.The syntax of union is as follows.
Syntax: Syntax:
struct tag_name struct tag_name
{ {
data_type member_1; data_type member_1;
data_type member_2; data_type member_2;
data_type member_3; data_type member_3;
| |
| |
| |
| |
data_type member_n; data_type member_n;
}structurevariable; }unionvariable;
2.In the structure the memory is 2.The memory is allocated to the
allocated when the structure variable is largest data member in the union.
created.
3.All the members of a structure can 3.Values assigned to one member may
be assigned values at a time. cause the change in the value of other
members.
4.All the members can be assigned at 4.Only the one union member can
the same time. initialized at a time.