team 2

23
IMPLEMENTATION OF VARIANT RECORDS

Upload: sathasivam-r

Post on 02-Aug-2015

33 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Team 2

IMPLEMENTATION OF VARIANT RECORDS

Page 2: Team 2

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)

Page 3: Team 2

CONTENTS INTRODUCTION STRUCTURE SYNTAX OF STRUCTURE VARIANT RECORD

UNION SYNTAX OF UNION REAL TIME EXAMPLE IMPLEMENTATION OF VARIANT RECORD CONCLUSION UNION Vs STRUCTURE REFERENCE

Page 4: Team 2

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.

Page 5: Team 2

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.

Page 6: Team 2

SYNTAX OF STRUCT

struct [structure tag]{

member1_declaration; member2_declaration; member3_declaration;

. . . memberN_declaration;

}; [one or more struct variables]

keyword

Struct name

Variable declaration

Page 7: Team 2

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 .

Page 8: Team 2

• 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)

Page 9: Team 2

union [union tag] {

member1_declaration; member2_declaration;

member3_declaration; . . . memberN_declaration;

}; [one or more union variables]

SYNTAX OF UNION

Union Name

keyword

Variable declaration

Page 10: Team 2

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.

Page 11: Team 2

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.  

Page 12: Team 2

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 ;

};

Page 13: Team 2

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;

Page 14: Team 2

EXAMPLE PROGRAM(Cont)

struct { int vehiclededuct; char license[10];

char state[2]; char model[15];

int year; } vehicle;

struct{ int homededuct; int yearbuilt;

} home;}

Page 15: Team 2

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.

Page 16: Team 2

EXAMPLE FOR IMPLEMENTATION

#define INTEGER 1

#define REAL 2

struct stint

{

int f3,f4;

};

struct stfloat

{

float f5,f6;

};

Page 17: Team 2

EXAMPLE FOR IMPLEMENTATION

struct sample { int f1; float f2; int utype; union {

struct stint x; struct stfloat y;

} funion;};

Page 18: Team 2

UNION Vs STRUCT

Page 19: Team 2

EXAMPLE UNION Vs STRUCTURE

Page 20: Team 2

• 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

Page 21: Team 2

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.

Page 22: Team 2

QUERIES…

Page 23: Team 2

THANK YOU…