cpu : structures and unions

23
STRUCTURE UNIONS

Upload: dhrumil-patel

Post on 18-Jul-2015

130 views

Category:

Engineering


3 download

TRANSCRIPT

Page 1: CPU : Structures And Unions

•STRUCTURE

•UNIONS

Page 2: CPU : Structures And Unions

1. INTRODUCTION

C’ Supports a constructed data type known as STRUCTURE, Which is a method of packing data of different types.

WHAT IS STRUCTURE ?

A Structure is a convenient tool for handling a group of logically related data items.

i.e. It can be used to represent a set of attributes, such as : student_name , roll_no

Page 3: CPU : Structures And Unions

A structure defination creates a formet that may be used to declare structure variables.

i.e. Consider a book database consisting of a book name, Author, Number of pages and price.

We can define a structure to hold this information as follows:

Page 4: CPU : Structures And Unions

Struct tag_name

{

data_type member1;

data_type member2;

… …

……… …..

};

Page 5: CPU : Structures And Unions

1. The template terminated with a semicolon

2. While the entire declarationn considered as a satement, each member is declared indepandently for its name and type in a separate statement inside the teplate.

3. The tag name such as book_bank can be used to declare structure variables of its type,letter in the program.

Page 6: CPU : Structures And Unions

Struct book_bank

{

Char title[20];

Char author[15];

Int pages;

Float price;

};

Page 7: CPU : Structures And Unions

The keyword struct declares a structure to hold the details of four fields, namely title,author,pages and price.

These fields are called structure elements or members and each member belong to different type of data.

Book_bank is the name of the structure and also called ‘STRUCTURE TAG’.

Page 8: CPU : Structures And Unions

title

author

pages

price

Array of 15 characters

integer

float

Array of 20 characters

Page 9: CPU : Structures And Unions

The link between a member and variable is established using the member operator ‘.’ which is also known as ‘dot operator’ or ‘period operator’.

i.e.

book1.price

Is the variable represnting the price of book1 and can be treated like any other ordinary variable.

Page 10: CPU : Structures And Unions

Here is how we would assign values to the members of book1:

strcpy(book1.title, “COMPUTER”);

strcpy(book1.author, “XYZ”);

book1.pages=250;

book1.price=29.99;

We can also use scanf to give the values through keyboard.

Page 11: CPU : Structures And Unions

A Structure must be declared as static if it is to be initialized inside a function.

main() {

static struct{

int weight;float height;

}student = (60,180.75);…..}

Page 12: CPU : Structures And Unions

If there are fewer initialization than that of member variables in the structure. The remaining member variables are initialized to zero.

i.e. if we don’t know the number of pages in book:

struct book b1={“let us C”,”kanetkar”,0,150.50};

Page 13: CPU : Structures And Unions

We use structure todescribe the format of a number related variables.

In such cases, we may declare array of structures,each element of an array represent a structure variable

i.e. struct class student[100]

This defines an array called student , that consists of 100 element.

Page 14: CPU : Structures And Unions

Each element is defined to be of the type struct class.

An array of structure is stored inside the memory in the same way as a multi-dimensional array.

Page 15: CPU : Structures And Unions

C permits the use of array as a structure members.

We can use single or multi-dimensional array of type int or float.

i.e. Struct marks

{

int number;

float subject[3];

} student[2];

Page 16: CPU : Structures And Unions

The main philosophy of c language is the use of functions. C supports the passing of structure values as arguments to function.

There are 3 methods by which the values of a structure can be transfferd from one function to another:

1. To pass each member of the structure as an argument of function call.

Page 17: CPU : Structures And Unions

2. Passing of a copy of the entire structure to the called function

3. Pointers to pass the structure as an argument.

General formet of sending a copy of a structure to thr called function:

data_type function name(st_name)

srtuct_type st_name;

{

return(expression);

}

Page 18: CPU : Structures And Unions

Unions are a concept borrowed from structures and therefore follow the same syntax as structures.

Major diffferance in terms of Storage:

In structures each member has its own storage location

In unions all members use the same location

Page 19: CPU : Structures And Unions

Union may contain many members of different type but can handle only one member at a time.

It can be declared using keyword union as follows:

union item

{

int m;

float x;

char c;

} code;

Page 20: CPU : Structures And Unions

Union union_name

{

data_type member1;

data_type mrmber2;

… … .. …..

} var1, var2, .. ;

Page 21: CPU : Structures And Unions

Union book;

{

char title[15];

char *author;

int pages;

float price;

} b1, b2, b3;

Page 22: CPU : Structures And Unions

A union variable can be assigned to another union variable.

Address of the union variable is obtained using the address of ‘&’ operator.

It is to pass a union to function and a function can return a union.

We can use pointer to unions and within unions.

Page 23: CPU : Structures And Unions

All members share the same storage area in computers memory.