S. Kiran, PGT (CS) KV, Malleswaram

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
UNIONS IN C.  Union Data Type Union Data Type  Defining of Union Defining of Union  Memory Space Allocation Memory Space Allocation  Example of Union.
EC-211 DATA STRUCTURES LECTURE 2. EXISTING DATA STRUCTURES IN C/C++ ARRAYS – A 1-D array is a finite, ordered set of homogeneous elements – E.g. int a[100]
Structure.
Pointer to Structure. Structure variable can be access using pointers int a=10,*p; Here p  is an integer type pointer variable, p can hold the address.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
Presentation on Structure. Structure Structure - It is a collection of variables referenced under one name. The keyword struct tells the compiler that.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Advanced Data types and Sorting
Structure A structure is a collection of variables of different data type under one name. It is a derived data type. e.g. struct employee {int empno; char.
Structures and Functions Ghulam Nasir Khan. Important /\/otes Programming is a fundamental course of IT as well as BICSE, so it will be very difficult.
UNIT II. -set of homogeneous data items. Eg: int arrays can hold only integers and char arrays can only hold characters. Arrays have a type, name, and.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Array, Structure and Union
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
DATA STRUCTURE & ALGORITHMS Pointers & Structure.
What is a function? Functions are nothing but sub-programs of a program. Is a part of a program which performs a particular task as desired by the programmer.
Struct Data Type in C++ What Are Structures?
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
FLOW OF CONTROL In C++ the program execution starts with the first statement in the main() and ends with the last statement of main(). This is called.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Array in C# Array in C# RIHS Arshad Khan
CS1010 Programming Methodology
Chapter 10-1: Structure.
CHP-2 ARRAYS.
Operator Overloading.
User Defined Data Types - Structures in C
Structures and Unions.
Lecture-5 Arrays.
Visit for more Learning Resources
C++ Arrays.
Structures.
DATA HANDLING.
Buy book Online -
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Structures Lesson xx In this module, we’ll introduce you to structures.
Structure ការណែនាំអំពី Structure
Chapter 10: Records (structs)
Arrays November 8, 2017.
Lecture 12 Oct 16, 02.
Introduction To Programming Information Technology , 1’st Semester
Multidimensional Arrays
Struct Data Type in C++ What Are Structures?
Chapter 1: Introduction to Data Structures(8M)
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Reference semantics, variables and names
Structure.
CHAPTER 2 Arrays and Vectors.
Arrays in Java.
By Yogesh Neopaney Assistant Professor Department of Computer Science
C Programming Lecture-8 Pointers and Memory Management
C Programming Lecture-13 Structures
Structures In C Programming By Rajanikanth B.
Structures.
CHAPTER 2 Arrays and Vectors.
The Pointers of Structures
C Programming Lecture-14 Unions
Structure (i.e. struct) An structure creates a user defined data type
Arrays Imran Rashid CTO at ManiWeber Technologies.
CSCE 206 Lab Structured Programming in C
Chapter 5 POINTERs Visit to more Learning Resources.
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
Arrays and Pointers.
Programming Fundamental
Presentation transcript:

S. Kiran, PGT (CS) KV, Malleswaram STRUCTURES

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.

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.

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

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

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.

S. Kiran, PGT (CS) KV, Malleswaram 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 S. Kiran, PGT (CS) KV, Malleswaram STRUCTURE DECLARATION Syntax : struct structure tag / name { datatype variablename; };

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 ;

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.

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

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

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;

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.

MEMORY ALLOCATION FOR STRUCTURE S. Kiran, PGT (CS) KV, Malleswaram MEMORY ALLOCATION FOR STRUCTURE EMPNO Name basic 0,0 . 9,0

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”;

S. Kiran, PGT (CS) KV, Malleswaram recapitulation

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?