DAY – 8
BASICS OF C
What will you learn?
Array of Structures
Arrays within Structures
Nested Structure
Self – Referential Structure
ARRAY OF STRUCUTRES
It is collection of structures those of same type. In other words elements of
an array are structures. If we are required to deal with 5 employees’ details, we
would declare an array of struct employee of size 5 as follows.
struct employee
int empno;
char name[20];
float salary;
}
A structure array is declared as follows:
struct employee e[5];
MEMORY REPRESENTATION
Empno name salary
e[0] 2 bytes 20 bytes 4 bytes
e[1] 2 bytes 20 bytes 4 bytes
e[2] 2 bytes 20 bytes 4 bytes
e[3] 2 bytes 20 bytes 4 bytes
e[4] 2 bytes 20 bytes 4 bytes
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 sub[3];
int total;
};
struct marks student;
In the above structure, the member sub contains three elements such as sub[0],
sub[1] and sub[2].These elements can be accessed by using the subscripts. For
example [Link][2], indicates the marks obtained by student and third
subject.
NESTED STRUCTURES
A Structure can be placed within another structure.
This means a structure contains another structure as its member.
A structure that contains another structure as its member is called nested
structure.
A structure can be nested as follows:
struct date struct employee
{ {
int day; int empno;
int month; char name[20];
int year; float salary;
}; struct date dob;
};
SELF REFERENTIAL STRUCTURES
Self Referential Structure is a structure that contains a pointer
to a structure of the same type as that of the structure, as its member.
Eg:
struct node
{
int value;
struct node *next;
};
Here the structure contains two data items, one is the value, and the
other is next which is pointer to the structure of type node.