arrays of structures

8

Upload: arushi-bhatnagar

Post on 12-Jul-2015

29 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: arrays of structures
Page 2: arrays of structures

What is array ?

An array is a collection of variables of the same data type that are referenced by a common name .

Eg:

int arr[3];

0 (2bytes) 1(2bytes) 2(2bytes)

arr[0] arr[1] arr[2]

Page 3: arrays of structures

What is structure ?

A structure is a grouping of several variables of different

data types. Struct keyword is used.

Eg:

struct book

{ int page; (2bytes) (15bytes) (4bytes)

char author[15]; page author price

float price;

};

Page 4: arrays of structures

What is Arrays of structures ?

Since an array can contain similar elements , the

combination having structures within array is an

array of structures .

Page 5: arrays of structures

Arrays of structures

Eg:

struct fraction

{

int numerator; // 2 bytes

int denominator; //2 bytes

};

Struct fraction f1[4]; // this is an array of structure

Page 6: arrays of structures

Memory allocation of arrays of structures

Total size of f1 = 4*2*2=16 bytes

0 (4) 1 (4) 2 (4) 3(4)

N D N D N D N D

f1[0] f1[1] f1[2] f1[3]

Page 7: arrays of structures

Accessing the elements of array of structure

Struct fraction f1[4];

For(i=0;i<4;i++)

{

Printf(“%d”,f1[i].numerator);

Printf(“%d”,f1[i].denominator);

}

Page 8: arrays of structures

Thank youAny queries ?

prepared by :

Arushi