Chap 10(structure and unions)

11
Structure and Union Topics Introduction Structure Definition Giving values to Members Structure Initialization Array of Structures Arrays within Structure

Transcript of Chap 10(structure and unions)

Page 1: Chap 10(structure and unions)

Structure and Union

Topics

Introduction

Structure Definition

Giving values to Members

Structure Initialization

Array of Structures

Arrays within Structure

Page 2: Chap 10(structure and unions)

Introduction

Arrays can be used to represent a group of

data items that belong to the same type.

However, if we want to represent a collection

of data items of different types using a single

name then we cannot use an array.

C supports a user defined data type

structure for packing data of different types.

A structure is a convenient tool for handling

a group of logically related data items.

Page 3: Chap 10(structure and unions)

Structure Definition The general format of structure is:

struct tag_name

{

data_type member1;

data_type member2;

-----------

-----------

};

Ex: struct student

{

char name[20];

int roll;

float gpa;

};

Page 4: Chap 10(structure and unions)

Continue…

We can declare structure variables using the tag name anywhere in the program. For example:

struct student s1,s2,s3;

Each of the variable has three members and the members are not variable.

The declaration can be:

struct student

{

char name[20];

int roll;

float gpa;

}s1,s2,s3;

Page 5: Chap 10(structure and unions)

Giving values To members

The link between a member and a variable is established using the member operator (.).

For example: s1.name is the variable representing the name of s1 and can be treated like any other ordinary variable.

We can assign values to the members of s1:

strcpy(s1.name, “Rahul”);

s1.roll=1;

s1.gpa=3.79;

We can use scanf to give values through the keyboard.

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

scanf(“%d”,&s1.roll);

Page 6: Chap 10(structure and unions)

Structure Initialization

Like any other data type, a structure variable can be initialized. A structure must be declared as static if it is to be initialized inside a function.

Ex:

main()

{

static struct

{

int x;

float y;

}str={101,350.789};

---------

}

Page 7: Chap 10(structure and unions)

Continue..

Another method is to initialize a structure variable outside the function:

struct st

{

int x;

float y;

}s={50,101.56};

main()

{

static struct st s1={200,340.50};

------------

}

Page 8: Chap 10(structure and unions)

Array of Structure

We use structures to describe the format of

a number of related variables. We may

declare an array of structures, each element

of the array representing a structure variable

For example:

struct student st[100]; defines an array

called st that consists of 100 elements.

Each element is defined to be of the type

struct student.

Page 9: Chap 10(structure and unions)

Structures within Structures Structure within a structure means nesting of structures.

Ex:

struct st

{

char name[20];

char dept[20];

struct

{

int roll;

float gpa;

}info;

}student;

We can use the members like student.info.roll, student.info.gpa, student.name

Page 10: Chap 10(structure and unions)

Continue…

We can also use tag names to define inner structures. Example-

struct info

{

int roll;

float gpa;

};

struct st

{

char name[20];

struct info str;

};

struct st student[20];

Page 11: Chap 10(structure and unions)

Union

Unions are the same syntax as the structure

However, there is major distinction between

them in terms of storage.Structure Union

Every member of the

structure occupy the

memory space

The space of the largest

member will occupy for

the union

There is no limitation for

using the member

Only one member can

handle at a time