You are on page 1of 18

Jr CSE Unit-V Structures

UNIT 5:STRUCTURES IN ‘C’

Structure-defination:

Defination1:A group of one or more variables of different data types organized


under a single name is called as a structure.

Defination2:A collection of heterogeneous data elements grouped together


under a single name is called as structure.

Declaration of structure:

 To define a structure we use “struct” key word.


 It is usually defined near to the start of a file.
 Structures must be defined first and then declared.

Syntax:

struct tag_name

{
data_type member_1;
data_type member_2;
data_type member_3;

|
|
|
|
data_type member_n;
};

Example1:

National Junior college Programming in ‘C’ Page 1


Jr CSE Unit-V Structures

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:

To explain a strucuture variable let’s take an example.

struct employee

National Junior college Programming in ‘C’ Page 2


Jr CSE Unit-V Structures

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:

eno[2 bytes] ename[10][10 bytes] salary[4 bytes]

For e2:

eno[2 bytes] ename[10][10 bytes] salary[4 bytes]

For e3:

eno[2 bytes] ename[10][10 bytes] salary[4 bytes]

Like the above the e1,e2,e3 are taken as the referential objects to the structure
employee.

Structure variable initialization:

Intialization1:

struct employee

National Junior college Programming in ‘C’ Page 3


Jr CSE Unit-V Structures

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:

Write a program to intilize a student information and display the


information.
National Junior college Programming in ‘C’ Page 4
Jr CSE Unit-V Structures

Program:

#include<stdio.h>

#include<conio.h>

struct student

int sno;

char sname[20];

int marks;

float avg;

};

main()

struct student s1={1,"revanth”,367,61.1};

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

National Junior college Programming in ‘C’ Page 5


Jr CSE Unit-V Structures

sno=1

sname=revanth

marks=367

avg= 61.14

Output:

Structures with arrays:

The arrays are used in structures in the following manner.

1.Array of structures

2.Structures containing arrays

3.Arrays of structures containing arrays

1.Arrays of structures:

 Here the structure variable contains the array structure.


 For example if we create the structure as student, that containing
variables sno,sname,smarks,if we want to create 50 student information
then we declare the structure variable as follows.

struct student

int sno;

char sname[10];

float smarks;

National Junior college Programming in ‘C’ Page 6


Jr CSE Unit-V Structures

}s[50];

Example:

Write a program to print 3 student marks and average by using structure

Program:

struct student

char name[20];

int sub1,sub2,sub3,total;

float avg;

};

main()

struct student s[3];

int i;

clrscr();

for(i=0;i<=2;i++)

printf("enter student name\n");

scanf("%s",s[i].name);

printf("enter student marks\n");

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;

National Junior college Programming in ‘C’ Page 7


Jr CSE Unit-V Structures

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:

enter student name

narasimha

enter student marks

71

62

63

enter student name

khan

enter student marks

84

National Junior college Programming in ‘C’ Page 8


Jr CSE Unit-V Structures

62

90

enter student name

dinesh

enter student marks

73

52

91

**************student information*******************

name=narasimha

sub1=71 sub2=62 sub3=63

total=196

average=65.000000

**************student information*******************

name=khan

sub1=84 sub2=62 sub3=90

total=236

average=78.000000

**************student information*******************

name=dinesh

sub1=73 sub2=52 sub3=91

total=216

average=72.000000
National Junior college Programming in ‘C’ Page 9
Jr CSE Unit-V Structures

2.Structre containing arrays:

If we create a structure variable as an array then it became under this type.

Example:

struct student

int sno,total;

char name[10];

int marks[6],avg;

}s1;

Example:

Write a program to print a student information having 6 subjects.

Program:

#include<stdio.h>

struct cse

char sname[20];

int sub[6];

int total;

float avg;

National Junior college Programming in ‘C’ Page 10


Jr CSE Unit-V Structures

};

main()

struct cse s;

int i;

s.total=0;

clrscr();

printf("enter student name\n");

scanf("%s",s.sname);

printf("enter student marks\n");

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

enter student name

joel

enter student marks

74 71 63 68 71 63

student details

name=joel

subject marks

74 71 63 68 71 63

total=410

average=68.000000

3.Array of structure containing arrays:

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:

A structure containing another structure in it is called as nested structure.

National Junior college Programming in ‘C’ Page 12


Jr CSE Unit-V Structures

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();

printf("enter student roll no\n");

scanf("%d",&s.rollno);

printf("enter student name\n");

scanf("%s",s.sname);

printf("enter date of birth\n");

scanf("%d%d%d",&s.dob.d,&s.dob.m,&s.dob.y);

printf("enter date of joining\n");

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:

enter student roll no

101

enter student name

raja

enter date of birth

10 9 1991
National Junior college Programming in ‘C’ Page 14
Jr CSE Unit-V Structures

enter date of joining

26 9 2008

---------student details---------

rollno=101

student name=raja

date of birth=10-9-1991

date of joining=26-9-2008

Unions:

An union is a collection of heterogeneous data elements under a single name.It


is same as structure but the difference is in the way of memory allocated to the
variables.

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:

Write a program for defining unions

Program:

union employee

int id;

char name[10];

Float salary;

} e1;

main ()

clrscr();

printf("enter employee details\n");

National Junior college Programming in ‘C’ Page 16


Jr CSE Unit-V Structures

printf("enter employee id\n");

scanf("%d",&e1.id);

printf("enter employee name\n");

scanf("%s",e1.name);

printf("enter employee salary\n");

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 details

Enter employee id

101

Enter employee name

sumanth

Enter employee salary

10000

Employee details

Id=16384

Name=

Salary=10000.000000
National Junior college Programming in ‘C’ Page 17
Jr CSE Unit-V Structures

Differences between structure and union:

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.

National Junior college Programming in ‘C’ Page 18

You might also like