5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture 21 5.3.2001.

Slides:



Advertisements
Similar presentations
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
Advertisements

StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
Programming in C Chapter 10 Structures and Unions
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
C Language.
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]
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.
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Programming and Data Structure
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
Topic 11 – Structures and Unions. CISC105 – Topic 11 Introduction to Structures We have seen that an array is a list of the same type of elements. A structure.
Heterogeneous Structures 4 Collection of values of possibly differing types 4 Name the collection; name the components 4 Example: student record harveyC.
1 CSE1301 Computer Programming Lecture 24 Structures (Part 2)
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Create your own types: typedef #define N 3 typedef double scalar; /* note defs outside fns */ typedef scalar vector[N]; typedef scalar matrix[N][N]; /*
Structures ANSI-C. Review: Data Structures Functions give us a way to organize programs. Data structures are needed to organize data, especially: –1.
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Multidimensional Arrays Lecture 20 4/3/2002.
Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
Today’s Material Aggregate Data Types: Structures and Unions
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
Structures 142 S -1 What is a structure? It is a collection of possibly different types Name the collection Name the components For example: a student.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
P-1 5/18/98 CSE / ENGR 142 Programming I Arrays of Structures © 1998 UW CSE.
Dynamic Allocation Review Structure and list processing
Chapter 10-1: Structure.
Structures and Unions.
CS150 Introduction to Computer Science 1
C Structures, Unions, and Enumerations
Heterogeneous aggregate datatypes
Array of Structures A structure holds only one record But a record
Sudeshna Sarkar 7th March 2017
Chapter 1: Introduction to Data Structures(8M)
C Structures, Unions, Bit Manipulations and Enumerations
Structures.
Structures, Unions, and Enumerations
Presentation transcript:

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur2 Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record Singhal name "V Singhal" rollno "00CS1001" classtest 14 midterm 78 final 73 grade ‘B

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur3 Structure : terminology A struct is a group of items (variables) which may be of different types. Each item is identified by its own identifier, each of which is known as a member or field of the structure. A struct is sometimes called a record or structure. Structs are the basis of classes in C++ and Java.

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur4 Structure declaration struct { char first[10]; char midinit; char last[20]; } sname, ename; This declaration creates two structure variables, sname and ename, each of which contains 3 members. We can use sname.first, ename.midinit, etc.

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur5 Members To access the members of a structure, we use the member access operator “.”. strcpy (sname.first, “Sudeshna”); sname.midinit = ‘K’; strcpy (sname.last, “Sarkar”) ;

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur6 Tagged structure struct nametype { char first[10]; char midinit; char last[20]; }; struct nametype sname, ename; typedef struct nametype NTYPE; NTYPE aname, bname; This definition creates a structure tag nametype containing 3 members: first, midinit, last. Variables may be declared of type struct. typedef is normally used to give names to a struct type.

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur7 typedef typedef struct { char first[10]; char midinit; char last[20]; } NAMETYPE; NAMETYPE sname,ename;

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur8 Another example #define MAX_NAME 40 typedef struct{ char name[MAX_NAME+1]; char rollno[10]; int classtest; int midterm; int final; char grade; } StudentRecord; Defines a new data type called StudentRecord. Does not declare a variable.

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur9 Declaring struct variables /* typedef structs go at top of program */... int..... float.... StudentRecord s1; StudentRecord singhal ; /* StudentRecord is a type; s1 and singhal are variables*/ struct nametype aname; /* struct nametype is a type; aname is a variable */

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur10 Things you can and can't do You can Use = to assign whole struct variables You can Have a struct as a function return type You cannot Use == to directly compare struct variables; can compare fields directly You cannot Directly scanf or printf structs; can read fields one by one.

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur11 Struct initializers /* typedef structs go on top */ StudentRecord s1 = {"V Singhal", "00CS1002", 15, 78, 73, 'B'}; Using components of struct variables s1.classtest = 46; s1.midterm = 78; scanf ("%d", &s1.rollno) ;

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur12 Assigning whole structs s1 = singhal; is equivalent to strcpy(s1.name, singhal.name) ; strcpy(s1.rollno, singhal.rollno; s1.classtest = singhal.classtest; s1.midterm = singhal.midterm; s1.final = singhal.final; s1.grade = singhal.grade;

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur13 Within a given structure, the member names must be unique. However, members in different structures may have the same name. A member is always accessed through a structure identifier. struct fruit { char name[20]; int calories; }; struct vegetable { char name[30]; int calories; }; struct fruit mango; struct vegetable potato; It is clear that we can access mango.calories and potato.calories without any ambiguity.

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur14 Complicated structures A member of a structure can be an array or another structure. struct grocerylist { struct fruit flist[10]; struct vegetable vlist[20]; } ; You can have an array of structures. struct card { int pips; char suit; } deck[52] ;

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur15 A function using struct array int fail (StudentRecord slist []) { int i, cnt=0; for (i=0; i<CLASS_SIZE; i++) cnt += slist[i].grade == ‘F’; return cnt; }

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur16 Using structures with functions Structures can be passed as arguments to functions. Structures can be returned from functions. Call by value is used if a structure is a function parameter, meaning that a local copy is made for use in the body of the function. If a member of the structure is an array, then the array gets copied as well. If the structure is large, passing the structure as an argument can be relatively inefficient. An address of th structure may be used as the parameter.

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur17 Union A union is like a structure, except that the members of a union share the same space in memory. union int_or_float { int i; float f; }; It is the programmer’s responsibility to know which representation is currently stored in a union variable.

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur18 Arrays of Structures A struct represents a single record. Typically structs are used to deal with collections of such records Examples : student records, employee records, book records,... In each case we will hav multiple instances of the struct type. Arrays of structs are the natural way to do this.

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur19 Arrays of structs : declaration & use Each declaration below declares an array, where each array element is a structure: point corner_points[10] ; StudentRecord btech01[MAXS] ; We access a field of a struct in an array by specifying the array element and then the field : btech01[i].name corner_points[4].x

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur20 Naming in struct Arrays point pentagon[5]; x y x y x y x y x y pentagon : an array of points pentagon[1] : a point structure pentagon[4].x : a double

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur21 Using Arrays of structs StudentRecord class[MAXS];... for (i=0; i<nstudents; i++){ scanf (“%d%d”, &class[i].midterm, &class[i].final); class[i].grade = (double)(class[i].midterm+class[i].final)/50.0; }

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur22 struct Array elements as parameters void draw_line (point p1, point p2) {... }... point pentagon[5];... for (i=0;i<4;i++) draw_line (pentagon[i], pentagon[i+1]); draw_line (pentagon[4], pentagon[0]);

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur23 structs as Parameters A single struct is passed by value. all of its components are copied from the argument (actual parameter) to initialize the (formal) parameter. point set_midpt (point a, point b) {... } int main (void) { point p1, p2, m;... m = set_midpt(p1, p2); }

5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur24 Passing Arrays of structs An array of structs is an array. When any array is an argument (actual parameter), it is passed by reference, not copied [As for any array] The parameter is an alias of the actual array argument. int avg (StudentRec class[MAX]) {... } int main (void) { StudentRec bt01[MAX]; int average;... average = avg_midpt(bt01) ; }