Download - 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]
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;
};
What is Arrays of structures ?
Since an array can contain similar elements , the
combination having structures within array is an
array 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
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]
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);
}
Thank youAny queries ?
prepared by :
Arushi