Data Structures Lecture 3: Struct Azhar Maqsood NUST Institute of Information Technology (NIIT)

Slides:



Advertisements
Similar presentations
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Advertisements

1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Summary of lectures (1 to 11)
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
1 Principles of Programming I Note Set #12. 2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
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.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Programmer Defined Structures (Records)
Learners Support Publications Classes and Objects.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Object-Oriented Programming in C++
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Array, Structure and Union
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Structures - Part II aggregate operations arrays of type struct nested structures compared to classes.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Data Structures Lecture 2: Array Azhar Maqsood NUST Institute of Information Technology (NIIT)
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
DATA STRUCTURE & ALGORITHMS Pointers & Structure.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
CSci 162 Lecture 6 Martin van Bommel. Functions on Structures Rather than pass a copy of structure to a function, or return a copy back as the function.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
Chapter Structured Data 11. Combining Data into Structures 11.2.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Procedural and Object-Oriented Programming
CO1401 Program Design and Implementation
Structures - Part II aggregate operations arrays of type struct
Dynamic Memory Allocation Reference Variables
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Classes and Objects.
Arrays Arrays A few types Structures of related data items
Structures Structured Data types Data abstraction structs ---
Pointers, Dynamic Data, and Reference Types
Structures, Unions, and Enumerations
Programming Fundamental
Presentation transcript:

Data Structures Lecture 3: Struct Azhar Maqsood NUST Institute of Information Technology (NIIT)

Structure This is the second Data structure that we will study and one of the powerful features of C language (made obselete by class in C++). Consider Student Data How to store names, age, address, city, CGPA for students Can u suggest how to create a database for it?

What is a Structure? A structure is an aggregate data structure used to keep different pieces of information together as a single data record. A structure is like an array except that each element can have a different data type, and the elements in a structure have names instead of subscript values

Declaring a struct Variable Syntax of the struct data type: struct name { type field_name1; type field_name2;... }; where – name is the name of the record type established by the struct, – each type field_name defines a field in the record, – the field definitions are separated by semicolons, and – the struct has a trailing semicolon.

Example of declaring a struct: /* declare a struct type (template) with name "Person_Record"*/ struct Person_Record { char name[20]; int age; }; /* Now, allocate memory */ struct Person_Record My_Record; /* a single record */ struct Person_Record PR_Array[100]; /* an array of records */

More about structures Normally structure is defined as global variable Its instances are created in main program or any other function The memory is not allocated when structure is defined. But when the instance of a structure is created then memory is reserved. Hence structure is like a cookie mold!  with no cookies unless you put the mixture of ingredients… Hence structure stand alone is a template… and it cannot be initialized. You need to Instantiate it or declare a variable of type structure

Initializing a Structure Variable To initialize a structure, follow the structure variable name with an equal sign, followed by a list of initialisers enclosed in braces. Example: struct Person_Record My_record = { "Julie Dagg", 32 };

Using a Structure Variable There are two ways to reference (access) the fields in a structure: The structure itself. A pointer to the structure.

I/O of Structures When you use cin/cout you must read/write a structure field-by-field. Emp.age = 20 Cin >> emp.age Cout << emp.age

Structures as Function Parameters The entire structure is copied to the function, i.e., a structure is passed by value. A pointer to a structure can be passed to achieve call-by-reference. Individual field of a structure can be passed as a real parameter to a function. It is possible for a function to return a structure or a pointer to a structure.

Examples: Passing an entire structure void func(Person_Record); //prototype of function... Person_Record My_Record; func(My_Record); /* call-by-value */ Passing a pointer to a structure void func(Person_Record *);... Person_Record My_record; func(&My_record); /* call-by-reference, pointer*/ Passing the address of a struct is faster, changes are reflected in My_Record

Nested Structures When one of the fields of a structure is itself a structure, it is called a nested structure. Struct DateofBirth { int years; int months; int day; }; Struct Emp { char[25] name; DateofBirth Dob; };

Self-Referential Structures (will do in detail in coming weeks) We can declare pointers to structures that have not yet been declared. These are called dynamic data structures. This permits us to create self-referential structures, as well as mutual-referential structures. Example: struct listnode { int nodedata; struct listnode *nextnode; };

Example Program Will do detailed program next time in the class! Here is a simple program

#include struct Date{ int mm; int dd; int yy; }; struct data{ char name[25]; int age; struct Date hireDate; }; void main(void) { data d[20]; //creates array of type data with limit 20 int i = 0; char ch; do { cout << "Please enter Name of the employee" << endl; cin >> d[i].name ; cout << "Please enter age" << endl; cin >> d[i].age ; cout << "Please enter date (dd,mm,yyyy)" << endl; cin >> d[i].hireDate.dd; cin >> d[i].hireDate.mm; cin >> d[i].hireDate.yy; cout << "Would like to enter more data? (y/n)" << endl; i++; cin >> ch; } while (ch != 'n'); for (int j=0; j < i; j++) { cout << endl << d[j].name; }}