team 2
TRANSCRIPT
IMPLEMENTATION OF VARIANT RECORDS
TEAM : 2
TEAM MEMBERS:
1. GEETHA .T (13MX12)2. KANNAN.S (13MX19)3. KATHIKEYAN.B (13MX22)4. MATHUMATHI.M (13MX28)5. PRASANTHI.P (13MX35)6. SURIYA LAKSHMI.V (13MX48)
CONTENTS INTRODUCTION STRUCTURE SYNTAX OF STRUCTURE VARIANT RECORD
UNION SYNTAX OF UNION REAL TIME EXAMPLE IMPLEMENTATION OF VARIANT RECORD CONCLUSION UNION Vs STRUCTURE REFERENCE
INTRODUCTION
WHAT IS A RECORD?
A record is a finite, ordered collection of possibly heterogeneous elements that are treated as a unit.
The elements of the record are commonly called fields.
A record is sometimes referred to simply as a structure.
STRUCTURE (RECORD)
A structure is a group of items in which each item is identified by its own identifier, each of which is known as a member of the structure.
In structure the total memory will be the sum of the memory of all the data types.
SYNTAX OF STRUCT
struct [structure tag]{
member1_declaration; member2_declaration; member3_declaration;
. . . memberN_declaration;
}; [one or more struct variables]
keyword
Struct name
Variable declaration
VARIANT RECORD
In C language record is called as structure (struct) and a variant record is called as union.
A variant record is one which permits a record variable to be interpreted in several different ways.
A variant record consist of two parts namely a fixed part and a variant part.
A fixed part consist of all member declaration up to the keyword union, while the variant part begins with the keyword union .
• A union is a variable type that may hold different type of members of different sizes. All members of the union share the same memory, but only one type can access at a time. The compiler assigns enough memory for the largest of the member types.
• The syntax for defining a union and using its members is the same as the syntax for a struct.
UNION (VARIANT)
union [union tag] {
member1_declaration; member2_declaration;
member3_declaration; . . . memberN_declaration;
}; [one or more union variables]
SYNTAX OF UNION
Union Name
keyword
Variable declaration
REAL TIME EXAMPLEINSURANCE POLICY:
Consider an insurance company which offers three kinds of policies to its customers like life, vehicle and home. Here the policy number acts a unique field for all three types of policies.
Apart from the policy number the other fields which are commonly needed namely:
Policy holder’s name.Address.Insurance amount.Premium.
REAL TIME EXAMPLE (Cont)
TYPE OF POLICY MEMBERS
Home policy Deductible amount, Age of the house and Security precautions.
Life insurance policy Insured’s birth date and Beneficiary.
Vehicle insurance policy
Deductible amount, License number, State, Model and Year.
EXAMPLE PROGRAM#define LIFE 1#define VEHICLE 2#define HOME 3struct addr
{ char street[50] ;
char city[10] ; char state[2] ; char zip[5] ; };struct date
{ int month ; int day ; int year ;
};
EXAMPLE PROGRAM(Cont)
struct policy {
int polnum ; char name[30]; strut addr address;
int amount; float premium; int kind; union {
struct { char beneficiary [ 30 ]; struct date birthday; } life;
EXAMPLE PROGRAM(Cont)
struct { int vehiclededuct; char license[10];
char state[2]; char model[15];
int year; } vehicle;
struct{ int homededuct; int yearbuilt;
} home;}
IMPLEMENTATION OF VARIANT RECORDS
A record type (structure) may be regarded as a road map to an area of memory. It defines how the memory is to be interpreted.
A variant record type (union) provides several different road maps for the same area of memory.
EXAMPLE FOR IMPLEMENTATION
#define INTEGER 1
#define REAL 2
struct stint
{
int f3,f4;
};
struct stfloat
{
float f5,f6;
};
EXAMPLE FOR IMPLEMENTATION
struct sample { int f1; float f2; int utype; union {
struct stint x; struct stfloat y;
} funion;};
UNION Vs STRUCT
EXAMPLE UNION Vs STRUCTURE
• Similarities– Definition syntax virtually identical– Member access syntax identical
• Differences– Members of a struct each have their
own address in memory. – The size of a struct is at least as big
as the sum of the sizes of the members .
– Members of a union share the same memory.
– The size of a union is the size of the largest member.
UNION Vs STRUCT
REFERENCEBOOKS:
1. DATA STRUCTURES USING C . - AARAON M. TENENBAUM,
YEDIDYAH LANGSAM, MOSHE J. AUGENSTEIN.
2. DATA STRUCTURES USING PASCAL. - AARAON M. TENENBAUM, MOSHE
J.AUGENSTEIN.
QUERIES…
THANK YOU…