18
UNIT-IV STRINGS AND DERIVED TYPES: UNIT-IV Objective: Structures provide a way to organize related data. Unlike arrays, structures allow organization of collection of variables. With different data types structures are very useful in creating data structures. Unions also provide a way to organize related data, but only one item within the union can be used at any time. The structures and unions in C are dealt in this unit. INTRODUCTION: We have seen arrays can be used to represent a group of data items that belong to the same type, such as int or float. However we cannot use an array if we want to represent a collection of data items of different types using the single name. C supports the constructed data type known as structures, a mechanism for packing data of different types. The concept of a structure is analogous to that of a record in many other languages. EX: Time Seconds(int),Minutes(int),Hours(float) M V B REDDY GITAM UNIVERSITY BLR

C UNIT-4 PREPARED BY M V BRAHMANANDA RE

Embed Size (px)

Citation preview

Page 1: C UNIT-4 PREPARED BY M V BRAHMANANDA RE

UNIT-IV STRINGS AND DERIVED TYPES:

UNIT-IV

Objective:

Structures provide a way to organize related data. Unlike arrays, structures allow

organization of collection of variables. With different data types structures are very useful in

creating data structures. Unions also provide a way to organize related data, but only one item

within the union can be used at any time. The structures and unions in C are dealt in this unit.

INTRODUCTION:

We have seen arrays can be used to represent a group of data items that belong to the same type, such

as int or float. However we cannot use an array if we want to represent a collection of data items of different

types using the single name. C supports the constructed data type known as structures, a mechanism for

packing data of different types. The concept of a structure is analogous to that of a record in many other

languages.

EX:

Time Seconds(int),Minutes(int),Hours(float)

Date Day (int),Month(string),Year(int)

Book Author(string),Title(string),Price(float)

Address Name(string),Doornumber(string),Street(string),City(string)

Structures help to organize complex data in a more meaningful way. It is a powerful concept

that we may often need to use in our program design.

STRUCTURES DEFINITION:

M V B REDDY GITAM UNIVERSITY BLR

Page 2: C UNIT-4 PREPARED BY M V BRAHMANANDA RE

A structure in C is heterogeneous compound data type, similar to the records of data base and

PASCAL. It is collection of logically related data items grouped together under a single name called

structure tag. The data items that make up a structure are known as its members, components, or fields

and can be of different type.

THE GENERAL FORMAT:

struct tag

{

type var1;

type var2;

type var3;

.

.

.

.

.

type varn;

};

Ex: struct book_bank

{

char title[25];

char author[20];

M V B REDDY GITAM UNIVERSITY BLR

Page 3: C UNIT-4 PREPARED BY M V BRAHMANANDA RE

int pages;

float price;

};

Ex: struct book_bank

{

char title[25];

char author[20];

int pages;

float price;

}book1,book2,book3;

ACCESSING STRUCTURE ELEMENTS:

The members of structure themselves are not variable.They should be linked to the structure

variable in order to make them meaningful members .The link between a member and a variable are

established using a number operator “.” which is also known as dot operator or period operator. For

example , book2.price is the variable representing the price of book2 and can be treated like any other

ordinary variables.

/* DEFINE A STRUCTURE TYPE, STRUCT PERSONAL, THAT WOULD

CONTAIN PERSON NAME, DATE OF JOINING AND SALARY, USING

THIS STRUCTURE, WRITE A PROGRAM TO READ THIS INFORMATION

FOR ONE PERSON FROM THE KEYBOARD AND PRINT THE SAME ON

THE SCREEN */

M V B REDDY GITAM UNIVERSITY BLR

Page 4: C UNIT-4 PREPARED BY M V BRAHMANANDA RE

#include<stdio.h>

struct personal

{

char name[20];

int day;

char month;

int year;

float salary;

main()

{

struct personal person;

clrscr();

printf(“ Enter a person details:\n\n”);

printf(“ Enter person name : “);

scanf(“%s”,person.name);

printf(“Enter a person joining day : “);

scanf(“%d”,&person.day);

printf(“Enter a person joining month: “);

scanf(“%d”,&person.month);

printf(“Enter a person joining year: “);

scanf(“%d”,&person.year);

M V B REDDY GITAM UNIVERSITY BLR

Page 5: C UNIT-4 PREPARED BY M V BRAHMANANDA RE

printf(“Enter a person salary: “);

scanf(“%d”,&person.salary);

printf(“\n\n person’s name is : %s\n”,person.name);

printf(“\n\n person’s joining day is : %s\n”,person.day);

printf(“\n\n person’s joining month is : %s\n”,person.month);

printf(“\n\n person’s joining year is : %s\n”,person.year);

printf(“\n\n person’s salary is : %s\n”,person.salary);

}

OUTPUT:

Enter a person details:

Enter a person name : Srinivas

Enter a person joining day: 9

Enter a person joining month: November

Enter a person salary: 5260.97

Enter a person joining year: 1997

Person’s name is : Srinivas

Person’s joining day is :9

Person’s joining month is : November

Person’s joining year is : 1997

Person’s salary is :5260.970215

STRUCTURE INITIALIZATION:

M V B REDDY GITAM UNIVERSITY BLR

Page 6: C UNIT-4 PREPARED BY M V BRAHMANANDA RE

Like other data types a structure variable can be initialized. However a structure must be declared

as static.

main()

{

static struct

{

int age;

float height;

}

student={20,180,75};

………………

………………

………………

}

This assigns the value 20 to student.age and 180.75 to student.height.

Suppose you want to initialize more than one structure variable:

main()

{

struct st_record

{

M V B REDDY GITAM UNIVERSITY BLR

Page 7: C UNIT-4 PREPARED BY M V BRAHMANANDA RE

int age;

float height;

};

static struct st_record student1={20,180,75};

static struct st_record student2={22,177,25};

…………………..

…………………..

}

Another method is to initialize a structure variable outside the function

struct st_record

{

int age;

float height;

}student1={20,180,75};

main()

{

static struct st_record student2={22,177,25};

………..

………..

COMPARISION OF STRUCTURE VARIABLE:

Two variables of the same structure type can be compared the same way as ordinary variables.

M V B REDDY GITAM UNIVERSITY BLR

Page 8: C UNIT-4 PREPARED BY M V BRAHMANANDA RE

/*PROGRAM YO ILLUSTRATE THE COMPARISION OF STRUCTURE VARIABLES*/

#include<stdio.h>

struct class

{

int no;

char name[20];

float per;

};

main()

{

int x;

static struct class stu1={111,”Ramu”,72.50};

static struct class stu2={222,”Reddy”,67.00};

struct class stu3;

stu3=stu2;

if(stu2.no==stu3.no&&stu2.per==stu3.per)

printf(“\n student2 and student3 are same\n”);

else

printf(“\n student2 and student3 are different\n”);

}

M V B REDDY GITAM UNIVERSITY BLR

Page 9: C UNIT-4 PREPARED BY M V BRAHMANANDA RE

ARRAYS OF STRUCTURES:

We may declare an array as structures , each element of the array representing a structure

variable. For example

struct class student[100];

Defines an array called ‘student’ that consists of 100 elements. Each element is defined to be of

the type struct class. Consider the following declaration

struct marks

{

int eng;

int tel;

int sci;

};

main()

{

static struct marks student[3]={45,76,87},{78,68,79},{34,23,14};

…………….

…………….

}

/*WRITE A PROGRAM TO CALCULATE THE SUBJECT-WISE AND STUDENT-WISE TOTALS AND STORE AS A

PART OF THE STRUCTURE*/

#include<stdio.h>

M V B REDDY GITAM UNIVERSITY BLR

Page 10: C UNIT-4 PREPARED BY M V BRAHMANANDA RE

struct marks

{

int tot;

int eng;

int tel;

int sci;

};

main()

{

int i;

static struct marks student[3]={{45,67,81,0},{75,53,69,0},{57,36,71,0};

static struct marks t;

for(i=0;i<3,i++)

{

student[i].tot=student[i].eng+student[i].tel+student[i].sci;

t.eng=t.eng+student[i].eng;

t.tel=t.tel+student[i].tel;

t.sci=t.sci+student[i].sci;

t.tot=t.tot+student[i].tot;

}

printf(“ STUDENT TOTAL \n\n”);

for(i=0;i<3;i++)

M V B REDDY GITAM UNIVERSITY BLR

Page 11: C UNIT-4 PREPARED BY M V BRAHMANANDA RE

{

printf(“ stu[%d] : %d\n”,i+1,stu[i].tot);

printf(“ SUBJECT TOTAL\n\n”);

printf(“English : %d\n Telugu : %d\n Science : %d\n”,t.eng,t.tel,t.sci);

printf(“\n Grand total : %d\n”,t.tot);

}

ARRAYS WITHIN STRUCTURES:

C permits the use of array as structure member. We can use single or multi-dimensional array

of type int or float.

struct marks

{

int no;

int sub[5];

float fee;

}stu[10];

STRUCTURES WITHIN STRUCTURES:

Structures within structures means nesting of structures .

struct employee

{

char name[30];

int age;

struct

M V B REDDY GITAM UNIVERSITY BLR

Page 12: C UNIT-4 PREPARED BY M V BRAHMANANDA RE

{

int day;

char month[20];

int year;

}j_date;

float sal;

}

UNIONS:

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

structures . However there is major distinction between them in terms of storage. In structures each

member has its own storage location, whereas all the members of a union use the same location. It can

handle only one member at a time.

General format:

union name

{

type var1;

type var2;

.

.

.

};

Ex:

union item

M V B REDDY GITAM UNIVERSITY BLR

Page 13: C UNIT-4 PREPARED BY M V BRAHMANANDA RE

{

int m;

float x;

char c;

}code;

This declares a variable code of type union item. The union contains three members each with a

different data type.

However we can use only one of them at a time. This is due to the fact that only one location is

allocated for a union variable, irrespective of its size.

65497 65498 65499 65500

c

m

M V B REDDY GITAM UNIVERSITY BLR

Page 14: C UNIT-4 PREPARED BY M V BRAHMANANDA RE

x

The compiler allocates a piece of storage that is large enough to hold the largest variable type

in the union.

In the declaration above the member x requires 4 bytes which is the largest among the

members. The above figure shows how all the three variables share the same address.

ACCESSING UNION ELEMENTS:

To access a union member we can use the same syntax that we used in the structure members.

Ex:

code.m;

code.x;

Pointers to remember:

Struct is a key word which shows the start of a structure.

A structure can be passed to as a single variable to function.

A union can also be stored as a member of the structure.

Pointers can also store the address of heterogeneous types of elements i.e., structures.

Key words:

Self referential structure.

Structure.

Union.

M V B REDDY GITAM UNIVERSITY BLR

Page 15: C UNIT-4 PREPARED BY M V BRAHMANANDA RE

Typedef.

Sample theory questions:

1) Distinguish between structure and union/

2) When an array of structures is used? Declare a variable as array of structure as initialize

it?

3) Write about passing of structures as arguments to functions?

Sample objective questions:

1) A bit field is of type integer.

2) C provides a facility for user defined new data type using typedef.

3) Structure is a derived data type derived.

4) Keyword used to represent a structure data type is Structure.

5) Structure is a data type in which each element that has different Data type.

6) The member variable of structure are accessed by using dot operator.

7) Union holds one object at a time.

M V B REDDY GITAM UNIVERSITY BLR