C Structures and Unions

23
STRUCTURE UNIONS

Transcript of C Structures and Unions

•STRUCTURE

•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

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:

Struct tag_name

{

data_type member1;

data_type member2;

… …

……… …..

};

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.

Struct book_bank

{

Char title[20];

Char author[15];

Int pages;

Float price;

};

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’.

title

author

pages

price

Array of 15 characters

integer

float

Array of 20 characters

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.

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.

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);…..}

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};

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.

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.

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];

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.

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

}

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

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;

Union union_name

{

data_type member1;

data_type mrmber2;

… … .. …..

} var1, var2, .. ;

Union book;

{

char title[15];

char *author;

int pages;

float price;

} b1, b2, b3;

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.

All members share the same storage area in computers memory.