Structures:
A structure is a derived data type. Structures can be made of members of unlile data type. The data item in a structure generally belong to same entity, like information of an employee, statistics of a player etc.
The structure declaration starts with the keyword struct, followed by list of member variables, their names and data types enclosed in braces followed by list of structures variables. The declaration of structure template caused no memory to be allocated. E.g.
Struct Circle
{
float cx;
floact cy;
float rad;
}
outer_circle;
Data type Circle is declared with three fields cx, cy, rad and a structure variable outer_Circle is defined tag circle can be used to define outer variables of this type.
Defining a Structure Variable:
Declaring structure template is naming a new data type. Memory allocated only on defining a variable of this new data type. E.g.
Structure definition
Struct accpount
{
Int acc_num;
Char acc_type;
Char name[20];
Float balance;
}
structure declaration:
struct account A; B;
Where A and B are two structures variables of type account. Total amount of memory allocated to each will be 87 bytes on 16 bit machines.
Referencing Member Variables of a Structure:
A member variable of a structure is referenced by a dot(.) operator. E.g.
Newcust.balance =1000.00;
Oldcust.acc_type = ‘D’;
Strcpy (old_Cust.name, “Employee Name”);
Newcust.name[0] = ‘R’;
Oldcust.acc_num++;
Scanf (“%d”,& oldcust.acc_num);
Initializing Structure Variables:
When a Structure variables is defined the memory for member variables is allocated hence initialization can be done at the time of defining a structure variable. Members of a structures are initialized inside the braces. E.g.
Struct acc customer = {100, ‘w’, “Cust Name”, 5000.00};
Copying Structure Variables:
A Structure variable can not be copied to another using assignment operator. Individual member variables have to copied. E.g.
Newcust.acc_num = oldcust.acc_num;
Newcust.acc_type = oldcust.acc_type;
Strcpy (newcust.name, oldcust.name);
Newcust.balalnce = oldcust.balance;
Nested Structures:
Member cariables of structure can be of any data type. It can even be a structure variable of another type. E.g
Struct date
{
int dd, mm, yy;
}
struct Acc
{
int acc_num;
char acc_type;
char name [80];
float balance;
struct date birth date;
}
struct acc new, old
in above case member variables of birth date structure variable can be accessed as:
new.birth_date.dd or old.birth_date.yy;
Passing Structure Variables to Functions:
When a structure variable is passed to function the entire structure variable is copied into receiving formal variables, which should be of the same data type. The structure variable can be a local variable, but structure template declaration should be global.
Struct date
{
int yy, mm, dd;
}
void main( )
{
struct date today;
fncall(today);
}
void fncall(struct Srw.dt)
{
dt.dd or dt.mm or dt.yy;
}
since, this is a copy of a original variable, changes made to member variables are not reflected in calling function.
Structure arrays :
Like other data types arrays can be of structure variables also, Structure has to be defined in normal way. Then an array can be declared of that data type. E.g.
Struct students
{
int rollnum;
char name[40];
float marks [6];
};
struct students student;
struct students student1;
This defines an array of 10 students. Each element of array is a structure of type struct students. Member variables from this array can be accessed as a structure variable except that the index is required for qualifying the elements from the array. E.g.
students1[0].rollnum = 0;
strcpy (students[9].name, “name”);
students1 [5].name [0] = ‘R’;
students[2].marks[0] = 100.0;
students [9].marks[1] = 22.50;
scanf(“%s”, students1[2].name);
scanf(“%f”,& students1[3].marks[5]);
pointers to Structures:
C allows pointers to variables of type structures also. Structure pointers can be declared as :-
struct students studptr;
studptr is a pointer to structure of type students. It can be used to access member variables indirectly using structure pointer operator () e.g.
studptr = &onestu;
scanf(“%s”,studptrname);
scanf(“%d”, &studptrrollnum);
scanf(“%d”,&studptrmarks[0]);
Type Definitions:
C language has a facility to give new names to data types with the keyboard typedef which does not create a new data type it only gives a new name to an existing type e.g.
typedef float Real;
typedef char Byte;
then a variable of these type can be defined using new names e.g.
Real balance,amount =100.0;
Byte flag = ‘y’;
Dynamic Memory Allocation in Structure Arrays:
malloc( ) function is used to allocate memory during runtime and free( ) is used to release the allocated memory so that it may be used by the system or the user program e.g.
typedef struct{
int empnum;
char name[100];
float salary;
}EMPREC;
EMPREC *strarry [20];
Strarry [0] =(EMPREC*)malloc(size of(EMPREC));
If (strarray[0] == 0)
{
printf(“malloc does not find enough memory”);
exit(1);
}
strcpy(strarry[0]name.”employ”);
strarry[0]salary =2500.00;
free (strarray[0]);
}
Self Referential Structures:
A structure declaration allows another structure to be a member variable of the structure. It does not allow the structure of same type to be a member variable since this would occupy infinite amount of memory.
E.g.
Struct info
{
int I, j, k;
char name[10];
struct info employee;
}
list;
when such a need arises a pointer to the parent type structure can be included in parent structure definition, this pointer can be used with member or pointer to access the member variables. E.g.
Struct info
{
int I, j, k;
char name[10];
struct info * employee;
}
list;
Unions:
It is a collection of mutually exclusive variables, which means all its member variables share same physical storage and only one variable is defined at any given time. Size of the
structure variables is equal to the sum total of the size of all its member variables, whereas size of union variable is the size of its largest member variable. A union is defined as:
Union utag
{
int num;
char ch;
};
union utag field;
The above union will have two bytes of storage. Variables num is accessed as field.num and ch as field.ch. At any given time only one of them can be referred to. Any changes to one of the variable will affect value of other variable dot(.) and pointer operators can be used on unions.
Unions use memory efficiently by using same memory to store all union variables of different types, which exist at mutually exclusive times and are to be used in program only once.
Enumerations:
Used to create source code legibility by providing identifiers for a class of related objects. The members of enumerations are automatically assigned consecutive constant values starting from 0, which can be freely used anywhere in the program. A variable can be declared to be enumerations variable of type tag. E.g.
enum days{Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
enum days today,holiday;
today =Tuesday;
holiday= Sunday;
clrscr( );
if(today <Sunday)
printf(“\n Working day, %d more day to /Sunday”,Sunday-Today);
else(“Enjoy Yourself, Today is Holiday”);
}
Structure Program
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void main( ){
int I=0, j=0, count =0;
struct student{
int roll_num;
char name[40];
int marks[6];
int total;
float percent;
}
std [100];
students *cptr [2];
cptr[0] =(students *)malloc(size of students);
if (cptr[0] = =0)
{
printf(“Not Enough Memory”);
}
printf(“Enter Number of students”);
scanf(“%d”,&count);
clrscr( );
for(i=0; i<count; i++)
{
std[i].tot =0;
printf(“\n Enter Roll Number :”);
scanf(“%d,&std[i].roll_num);
printf(“Enter Name”);
scanf(“%s”,std[i].name);
for(j =0; j<=5; j++)
{
printf(“Enter Marks”);
scanf(“%d”,std[i].marks[j]);
std[i].total = std[i].total+std[i],marks[j]);
}
std[i].percent = std[I].total/6;
}
clrscr( );
printf(“\t\t\t My Mary English Hight School\n”);
printf(“\t\t\t ----------------------------------------------”);
printf(“\n\t\t Performance Record”);
for(i =0; i<count; j++)
{
printf(“\n\n\t Roll Number%d\t”,std[i].roll_num);
printf(“\t\t\t Name %s\t”,std[i].name);
printf(“\n\t total marks%d\t”,std[i].total);
printf(“\n\t Percentage%f\t\n”,std[i].percent);
printf(“\t\t\t ----------------------------------------------”);
}
getch( );
No comments:
Post a Comment