Saturday, 31 August 2013

Array of structures | C Study Material for freshers



Array of structures:-

A structure is simple to define if there are only one or two element, but incase there are too many objects needed for a structure, for ex. A structure designed to show the data of each student of the class, then in that case, the away will be introduced. A structure declaration using array to define object is given below.
Stuct student
{
Char name [15];
Int rollno;
Char sex;
};
Student data[50];

Initializing array of structures:-
It is to be noted that only static or external variable can be initialized.
Ex: -
struct employee
{
int enum;
Char sex;
Int sal;
};
Employee data [3] = { { 146, „m?, 1000 },
{ 200, 'f', 5000},
{ 250, 'm',10000} };
If incase, the structure of object or any element of struct are not initialized, the compiler will automatically assigned zero to the fields of that particular record.
Ex: employee data [3] = { { 146,'m'} ,{ 200, 'f' },{250 ,'m'}};
Compiler will assign:-
Data [0] sal=0; data [1].sal=0; data [2]. Sal=0;

=>WAP to initialize the array of structure members & display
void main ()
{
int i;
Struct student
{
long int rollno;
char sex;
float height;
float weight;
};
struct student data [3] = { {121,'m',5.7,59.8},
{122,'f',6.0,65.2},
{123,'m', 6.0, 7.5} };
clrscr ();
printf (“the initialized contents are:”);
For ( i=0; i< =2; i++)
{
printf (“%d/n ** Record is \n “, data [i].rollno);
printf (“% c\n “, data [i] .sex);
printf (“%f\n”, data [i].height);
printf (“%f\n”, data [i]. weight);
}
}

Arrays with in the structures:-
Arrays can be used within the structure along with the member declaration. In other words, a member of a structure can be an array type also.
struct emp
{ char name [30] ;// 30 characters array .
char dept [30];
int sal;
char add[50];
};

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved