Downloaded S. from Kiran, www.studiestoday.com PGT (CS) KV, STRUCTURES
WHAT IS A STRUCTURE? Structure is a collection of logically related data. It is also a collection of dissimilar datatype.
Downloaded from S. www.studiestoday.com Kiran, PGT (CS) KV, WHY DO WE NEED A STRUCTURE? For instance, if we want to store the information of 50 employees. Such as employee number, name, age and basic.
Using arrays, the declaration would be as follows: int empno[50]; char name[50][20]; int age[50]; float basic[50] And the memory allocation would be as follows
empno Name 0............49 0,0 age 49,0 0............49
DRAWBACKS OF USING ARRAY In the declaration made above if we want to search for a particular employee detail say for e.g. emp who is stored in index no. 30. it has to make 30 searches to get empno, 30 searches to get name, 30 searches to get age and 30 searches to find basic.
Downloaded from S. www.studiestoday.com Kiran, PGT (CS) KV, So altogether to fetch one employee detail the system has to make 30+30+30+30 i.e. 120 searches. Which doesn t make the program efficient and makes the process of data retrieval and access very slow. This drawback is over come by using the user defined datatype - STRUCTURES
STRUCTURE DECLARATION Syntax : struct structure tag / name { datatype variablename; datatype variablename; datatype variablename; };
EXAMPLE : Structure name struct student { int stdno; char name[20]; int age; float total; char grade; }; Structure elements Remember structure declaration ends with a ;
MEMORY ALLOCATION No memory is allocated at the time of structure declaration. Memory is allocated only when a structure variable is declared.
STRUCT VARIABLE DECLARATION For the structure declaration made abovethe variable would be defined as follows : Syntax: structname variable ; For eg: student s ; Structure variable Structure name
MEMORY REPRESENTATION For a single structure variable memory is allocated as follows : stdn o name age total grade
Downloaded from S. www.studiestoday.com Kiran, PGT (CS) KV, REFERENCING STRUCTURE ELEMENTS The elements in a structure are referenced using dot operator. Eg., student s; s.stdno; s.name;
Downloaded from S. www.studiestoday.com Kiran, PGT (CS) KV, STRUCTURE AS ARRAY struct emp { int empno; char name[20]; float basic: } e[10]; Here an array to store 10 employee details is created.
Downloaded from S. www.studiestoday.com Kiran, PGT (CS) KV, MEMORY ALLOCATION FOR STRUCTURE EMPNO Name basic 0,0........ 9,0
Program: struct emp cin>>e[i].empno; { cout<< Enter Emp. Name ; int empno; cin>>e[i].name; char ename[20]; cout<< Enter Basic ; float basic; }e[10]; } void main() Downloaded from S. www.studiestoday.com Kiran, PGT (CS) KV, { } clrscr(); cout<< Enter emp details ; for(int i=0; i<10; i++) { cout<< Employee No ; cin>>e[i].basic; getch();
RECAPITULATION
Downloaded from S. www.studiestoday.com Kiran, PGT (CS) KV, What is the difference between an array and structure? What are the drawbacks of using an array? When is memory allocated for structure datatypes? How do we refer the elements of the structure?