0% found this document useful (0 votes)
54 views

UNIT-5 Structures & Unions

Structures allow grouping of related data types together under one name. A structure variable stores the data contiguously in memory. Structures can contain different data types and can be accessed using dot (.) operator. Arrays of structures allow storing multiple records of a structure. Structures can be passed to functions by value or by address. Self-referential structures contain a pointer member pointing to the same structure. Enumerated data types allow user-defined variable types. Unions share the same memory location for multiple data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

UNIT-5 Structures & Unions

Structures allow grouping of related data types together under one name. A structure variable stores the data contiguously in memory. Structures can contain different data types and can be accessed using dot (.) operator. Arrays of structures allow storing multiple records of a structure. Structures can be passed to functions by value or by address. Self-referential structures contain a pointer member pointing to the same structure. Enumerated data types allow user-defined variable types. Unions share the same memory location for multiple data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT-5 Structures & Unions

Structures-Definition: A structure can be defined to be a group of logically related data items,


which may be of different types, stored in contiguous memory locations, sharing a common
name, but distinguished a common by its members. Hence a structure can be viewed as a
heterogeneous user-defined data type.

2 B y te s

s tru c t e m p lo y e e
{ x ;
8 0 B y te s
in t e n o ;
c h a r n a m e [8 0 ];
flo a t s a l;
} ; 4 B y te s

S tru c t e m p lo y e e x ;

Declaring Structures:
The structure can be declared with the keyword, struct following the name and opening
brace with data elements of different type then closing brace with semicolon, as shown below.
Example
struct book
{
char name[10];
float price;
int pages;
};
struct book b1,b2,b3;
Accessing Structured Elements:
After declaring the structure type, variables and members, the member of the structure can be
accessed by using the structure variable along with the dot (.) operator.
Example: Program to print the student number, name and marks through accessing structure
elements.
#include<stdio.h>
struct std /* std is a structure with elements * /
{
int no;
char name[10];
int m1;
int m2;

1
int m3;
};
struct std s;
void main()
{
float total, avg;
printf("Enter Student details (Number, Name,Marks1,Marks2,Marks3)....\n");
scanf("%d%s%d%d%d",&s.no, &s.name, &s.m1, &s.m2, &s.m3);
total = s.m1 + s.m2 + s.m3;
avg = total/3;
printf("%d\t%s\t%.2f%.2f\t%.2f",s.no, s.name, total, avg);
}
Initialization of Structure
Example :
struct std
{
int sno;
float avg;
}
main()
{
struct std_rec std = {39,39.77};
struct std_rec std1 = {17,17.76};
}
Arrays of Structures:
Variable x is type struct student can store details (rollno, name, per) of a single student. If we are
required to deal with more than one students details say ten or even 20, declaration of so many
variables of struct student type and using those variables would not be a good idea. This is
because collective manipulations cannot be performed over them easily. We would naturally be
acceptable to use the concept of array of struct student type because of the flexibility provided by
arrays in performing collective manipulations. C does support arraying of structures.
Example: /*Program to illustrate array of structures.*/
#include<stdio.h>
struct student
{
int rollno;

2
char name[80];
float per;
};
void main()
{
struct student x[20];
int i,n;
printf("Enter no of students[1-20]\n");
scanf("%d",&n);
printf("Enter %d students details \n", n);
for(i=0;i<n;i++)
scanf("%d%s%f",&x[i].rollno,&x[i].name,&x[i].per);
printf("Student details\n");
for(i=0;i<n;i++)
printf("%3d%20s%9.2f\n",x[i].rollno,x[i].name,x[i].per);
getch();
}
Passing Structures to Function:
There are three ways available to pass structure variables to the function. They are,
i) Pass each member of the structure as an actual argument of the function call.
ii) Pass a copy of the entire structure to the called function.
iii) Through pointer pass the structure as an argument.
When a structure is used as a parameter to a function, the entire structure is passed to the
function using the call by value method. Here also, there is no change made effects on the
parameter.
Example: Program to passing a copy of entire structure to a function.
#include<stdio.h>
#include<conio.h>
struct std
{
int no;
float avg;
};
void fun(struct std p);
void main()
{
3
struct std a;
clrscr();
a.no=15;
a.avg=90.75;
fun(a);
}
void fun(struct std p)
{
printf("Number is .....%d\n",p.no);
printf("Average is .....%f",p.avg);
}
Self-Referential Structures
A structure consists of atleast a pointer member pointing to the same structure is known as a self-
referential structure. In general it can be expressed as,
Syntax :
struct tag
{
member 1;
member 2;
member 3;
.....
struct tag *name;
};
Where name, refers to the name of a pointer variable. Thus, the structure of type tag will contain
a member that points to another structure of type tag.
Example :
struct emp
{
int code;
struct emp *eptr;
}; /* eptr is a member, which is a points to struct emp. */
This is a structure of type emp. The structure contains two members one is integer type item code
another is a pointer to a structure of the same type (i.e. emp).Therefore this is a self referencial
structure.

Pointer to Structures

4
A pointer can be declared in such a way that it points to a structure data type.
Example: Program shows how the structure can be accessed by a pointer variable.
#include<stdio.h>
struct student
{
int roll_no;
char name[10];
float marks;
} stud 1;
main()
{
struct student * pt;
printf("Enter student details:\n");
scanf("%d%s%f", & stud1.roll_no, stud1.name, & stud1.marks);
pt=& stud1; /* Assigning * /
printf("\n Display of structure using structure variable\n");
printf("\n Roll_No Name Marks");
printf("\n _______ _____________ ____ \n");
printf("\n%d%s%.2f", stud1.roll_no, stud1.name, stud1.marks);
printf("\n Display of structure using pointer variable\n");
printf("\n Roll_No Name Marks");
printf("\n _______ _____________ ____ \n");
printf("\n%d %s %.2f", pt-->roll_no, pt-->name, pt-->marks);
}
Structure within structure or Nested structures
There is where the concept of nesting one structure within another comes into picture. Structure
if one type is made the members of another structure.
Here, the structure student contains the variable of structure date. The schematic view of this
embedded structure is given the following figure.

5
S tu d e n t
s tru c t d a te
{
ro lln o in t d a y ;
in t m o n th ;
n am e
in t y e a r;
p er } ;
s tru c t s tu d e n t
d o a {
in t ro lln o ;
d ay
ch ar n am e [8 0 ];
flo a t p e r;
m o n th
s tru c t d a te d o a;
y e ar } ;

Now, members of embedded structure can be accessed by using the following format:
External_struct_variable Internal_struct_variable.Member;

Enumerated Data Type:


The ANSI provides user defined data type which is called enumerated data type i.e., the
programmer can create their own data type and define what values the variables of these data
types can hold.
Example : /* Program to using enumerated data type */
#include<stdio.h>
main()
{
int roll1, roll2;
enum std {FIRST, SECOND, THIRD, FOURTH};
enum std s1,s2;
printf("Enter the roll nos. of 2students");
scanf("%d%d", & roll1, & roll2);
s1=FIRST;
s2=FOURTH;
printf("The rollno. %d is admitted to %d standard", roll1, s1 + 1);
printf("The rollno. %d is admitted to %d standard", roll2, s2 + 1);
}
Union
Union is a derived data type and it is declared like structure. The difference between union and
structure is in terms of storage. In structure each member has its own storage location, whereas
all the members of union use the same location, although a union may contain many members of
different types. When we use union the compiler allocates a piece of storage that is large enough
to hold. Like structures, union is also declared by using the keyword union.
Example : Program to find size of union and number of bytes reserved for it.
6
#include<stdio.h>
void main()
{
union result /* result is a union with elements * /
{
int marks;
char grade;
};
struct res /* res is a structure with elements * /
{
char name[15];
int age;
char sex;
char address;
int pincode;
union result perf;
}
clrscr();
printf("Size of Union : %d\n",sizeof(data.perf));
printf("Size of Structure : %d\n",sizeof(data));
getch();
}
TYPE DEF:
'C' provides a capability that enables the programmer to assign an alternate name to a data type.
This is done with a statement known as typedef or User-Defined Data Types.
Typedef, a facility provided by C, enables us to rename existing built-in data types and user-defined data
types and thereby helps in increasing the degree of readability of source code. The syntax is-
typedef Datatype NewName;
where datatype represents the existing datatype for which you want to specify an identifier and NewName
refers to that identifier.
Example: typedef unsigned int number;
We can now declare variables of unsigned int type using the new name as:
number x; x has been declared to be a variable of unsigned int type.

7
s tru c t s tu d e n t
/* E x a m p le : illu s tr a te th e c o n c e p t o f ty p e d e f* /
{
# in c lu d e < s td io .h >
in t r o lln o ;
v o id m a in ()
c h a r n a m e [8 0 ];
{
flo a t p e r;
ty p e d e f in t n u m b e r;
};
n u m b e r x , y , d iff;
ty p e d e f s tru c t s tu d e n t s td ;
x=20, y=10;
s td x ;
d iff = x -y ;
p r in tf ( " D iff e r e n c e o f % d a n d % d = % d \n " ,x ,y ,d if f ) ;
/* x h a s b e e n d e c la re d to b e a
}
v a r ia b le o f s tru c t s tu d e n t ty p e ;* /
Example: /* Program to create user defined data type and use it in the program * /
#include<stdio.h>
#include<conio.h>
#define D 7
void main()
{
typedef int weeks;
weeks wk;
printf("Enter weeks : ");
scanf("%d",&wk);
printf("Number of days = %d",wk* D);
getch();
}

BIT-FIELDS:
A bit-field is basically a set of adjacent bits in a word of memory. Members of structure can
include bit-fields. By making bit-fields as the members of a structure, we can combine several
items of information into a single word.
The bit-field is useful for storing the Boolean variables. Several such variables can be stored into
an individual word (which is int). Bit field is a member of structure and can be accessed as a
member of structure. Bit fields are defined in bits rather than other data types.

8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

day m o n th year N ot used


< > < > < > < >
T h r e e b it f ie ld w ith in o n e 1 6 -b it w o r d

# in c lu d e < s td io .h >
stru c t S tru c tu re N a m e ; {
{ E x a m p le u n s ig n e d d a y : 3;
D a ta T y p e m e m b e r-1 : s iz e ; s tru c t d a te u n s ig n e d m o n th :3 ;
D a ta T y p e m e m b e r-2 : s iz e ; { u n s ig n e d y e a r: 1 2 ;
.... u n s ig n e d day : 3; };
.... u n s ig n e d m o n th :3 ; v o id m a in ()
..... u n s ig n e d y e a r: 1 2 ; {
D a ta T y p e m e m b e r-n : s iz e ; }; s tru c t d a te d o j= { 2 0 ,1 0 ,2 0 1 1 } ;
}; s tru c t d a te d o j; p r in tf ( " D a te o f jo in in g = % d /% d /% d \n " , d o j.d a y , d o j.m o n th , d o j.y e a r ) ;
p rin tf(" d o j re q u ire s % d b y te s \n " , s iz e o f(d o j));
}

S tr u c tu re A rray
1. S tru c tu re is a u s e r-d e fin e d d a ta ty p e . 1 . A r r a y i s a d e r i v e d d a ta ty p e .
2. It c o n ta in s s e t o f d iffe re n t d a ta ty p e . 2 . It c o n ta in s s e t o f s a m e ty p e s .
3. I t u s e s d o t( .) o p e r a to r f o r a c c e s s in g e le m e n ts 3 . It u s e s s u b s c rip t fo r a c c e s s in g a rra y e le m e n ts .
4. E v e ry s tru c tu re e le m e n ts is a c c e s s e d a t d iffe re n t tim e . 4 . E v e ry a rra y e le m e n t is a c c e s s e d a t c o n s ta n t tim e .
5. A c c e s s in g ta k e s m o re tim e . 5 . S e a rc h in g / a c c e s s in g ta k e s le s s tim e .
6. D e c la ra tio n 6 . D e c la ra tio n
s tru c tu re ta g d a ta ty p e a rra y -n a m e [s iz e ];
{ = { lis t o f e le m e n ts s e p a ra te d b y c o m m a s } ;
d a ta ty p e m e m b e r1 ;
d a ta ty p e m e m b e r1 ;
};
7. It is k n o w n a s d y m a m ic m e m o ry a llo c a tio n . 7 . It is k n o w n a s s ta tic m e m o ry a llo c a tio n .

9
S tr u c tu re U n io n A rray
1 . S trc tu re is a u s e r-d e f in e d 1 . U n io n is a d e r iv e d d a ta ty p e 1 . A rra y is d e riv e d d a ta ty p e .
d a ta ty p e .
2 . It c o n ta in s s e t o f h e te ro g e n e o u s 2 . It c o n ta in s s e t o f e le m e n ts 2 . It c o n ta in s a s e t o f h o m o g e n e o u s
d a ta ty p e . b e lo n g in g to d iffe re n t d a ta ty p e s . d a ta ty p e s .
3 . E v e ry m e m b e r u s e s th e ir o w n 3 .A ll th e m e m b e r s u s e th e s a m e 3 . A ll th e m e m b e rs u s e c o n tin o u s
s to ra g e lo c a tio n fo r a c c e s s in g m e m o ry a llo c a tio n m e m o r y lo c a tio n s .
e le m e n ts .
4 . E v e ry s tru c tu re m e m b e r is 4 . T h e m e m o ry e q u iv a le n t to th e 4 . T h e m e m o ry a llo c a te d is e q u a l
a llo c a te d m e m o ry , w h e n s tru c tu re la rg e s t ite m is a llo c a te d c o m m o n ly to th e s iz e o f th e a rra y .
v a ria b le is d e fin e d . fo r a ll m e m b e r s .
5 . A ll th e m e m b e rs c a n b e a s s ig n e d 5 . O n ly o n e m e b e r c a n b e a s s ig n e d 5 . It is p o s s ib le to a s s ig n v a lu e to
v a lu e s a t a tim e . v a lu e a t a tim e . a ll th e m e m b e rs a t a tim e o r
to a s in g le m e m b e r.
6 . V a lu e s a s s ig n e d to o n e m e m b e r 6 . V a lu e s a s s ig n e d to o n e m e m b e r 6 . V a lu e a s s ig n e d to o n e m e m b e r
w ill n o t c a u s e th e c h a n g e in m a y c a u s e th e c h a n g e in o th e r is in d e p e n d e n t o f a n o th e r
o th e r m e m b e rs m em b ers m e m b e r v a lu e s .
7 . I t u s e d o t( .) o p e r a to r f o r 7 . I t u s e d o t( .) o p e r a to r f o r 7 . It u s e s s u b s c rip t fo r a c c e s s in g
a c c e s s in g e le m e n ts . a c c e s s in g e le m e n ts . a r r a y e le m e n ts
8 . M e m o ry c a n b e a llo c a te d 8 . M e m o ry c a n b e a llo c a te d 8 . M e m o ry s iz e c a n n o t b e c h a n g e d
d y n a m ic a lly . d y n a m ic a lly . o n c e d e f in e d i.e ., it is s ta tic
m e m o ry a llo c a tio n .
9 . T h e u s a g e o f " s tr u c tu re " is 9 . T h e u s a g e o f " u n io n " is 9 . T h e u s a g e o f " a rra y " is e ffic ie n t
e ffic ie n t w h e n a ll m e m b e rs e ffic ie n t w h e n m e m b e rs o f it w h e n m e m b e rs b e lo n g s to s a m e
a re a c tiv e ly u s e d in th e a re n o t re q u ire d to b e a c c e s s e d d a ta ty p e .
p ro g ra m a t th e s a m e tim e .
1 0 . D e c la ra tio n 1 0 . D e c la ra tio n 1 0 . D e c la ra tio n :
s tru c t ta g u n io n ta g d a ta ty p e a rra y _ n a m e [s iz e ]
{ { = { lis t o f e le m e n ts s e p a ra te d
d a ta ty p e m e m b e r1 ; d a ta ty p e m e m b e r1 ; by co m m as};
d a ta ty p e m e m b e r2 ; d a ta ty p e m e m b e r2 ;
} }
1 1 . E x a m p le : 1 1 . E x a m p le : 1 1 . E x a m p le :
s tr u c t s t u n io n u n in t a [5 ]= { 1 0 , 1 2 , 1 4 , 5 6 , 6 7 } ;
{ {
in t i; in t i;
flo a t f; flo a t f;
} s tr u c t s t s = { 2 , 4 .5 } ; } u n io n u n a = { 2 , 4 .5 } ;

10

You might also like