Download presentation
Presentation is loading. Please wait.
1
S. Kiran, PGT (CS) KV, Malleswaram
STRUCTURES
2
Structure is a collection of logically related data.
S. Kiran, PGT (CS) KV, Malleswaram What is a structure ? Structure is a collection of logically related data. It is also a collection of dissimilar datatype.
3
Why do we need a structure ?
S. Kiran, PGT (CS) KV, Malleswaram 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.
4
Using arrays, the declaration would be as follows: int empno[50];
S. Kiran, PGT (CS) KV, Malleswaram 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
5
empno Name age 0 …………………. . . . . . .. . .………. ………………….49 0,0 49,0
S. Kiran, PGT (CS) KV, Malleswaram empno Name age 0 ………………… ………. ………………….49 0,0 49,0 0 ………………… ………. ………………….49
6
Drawbacks of using array
S. Kiran, PGT (CS) KV, Malleswaram 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.
7
S. Kiran, PGT (CS) KV, Malleswaram
So altogether to fetch one employee detail the system has to make 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
8
STRUCTURE DECLARATION
S. Kiran, PGT (CS) KV, Malleswaram STRUCTURE DECLARATION Syntax : struct structure tag / name { datatype variablename; };
9
S. Kiran, PGT (CS) KV, Malleswaram
Example : Structure name struct student { int stdno; char name[20]; int age; float total; char grade; }; Structure elements Remember structure declaration ends with a ;
10
S. Kiran, PGT (CS) KV, Malleswaram
Memory allocation No memory is allocated at the time of structure declaration. Memory is allocated only when a structure variable is declared.
11
Struct variable declaration
S. Kiran, PGT (CS) KV, Malleswaram 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
12
Memory representation
S. Kiran, PGT (CS) KV, Malleswaram Memory representation For a single structure variable memory is allocated as follows : grade total stdno age name
13
Referencing structure elements
S. Kiran, PGT (CS) KV, Malleswaram Referencing structure elements The elements in a structure are referenced using dot operator. Eg., student s; s.stdno; s.name;
14
S. Kiran, PGT (CS) KV, Malleswaram
Structure as array struct emp { int empno; char name[20]; float basic: } e[10]; Here an array to store 10 employee details is created.
15
MEMORY ALLOCATION FOR STRUCTURE
S. Kiran, PGT (CS) KV, Malleswaram MEMORY ALLOCATION FOR STRUCTURE EMPNO Name basic 0,0 . 9,0
16
struct emp cin>>e[i].empno; { cout<<“Enter Emp. Name”;
S. Kiran, PGT (CS) KV, Malleswaram 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; cin>>e[i].basic; }e[10]; } void main() getch(); { } clrscr(); cout<<“Enter emp details” ; for(int i=0; i<10; i++) { cout<<“Employee No”;
17
S. Kiran, PGT (CS) KV, Malleswaram
recapitulation
18
What is the difference between an array and structure?
S. Kiran, PGT (CS) KV, Malleswaram 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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.