Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,

Slides:



Advertisements
Similar presentations
Structure.
Advertisements

Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Structures Spring 2013Programming and Data Structure1.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
1 Techniques of Programming CSCI 131 Lecture 24 Structs.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
Dale/Weems/Headington
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 Lecture 24 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;
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 Lecture 25 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
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 Structured Types, Data Abstraction and Classes.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
1 Chapter 10 Simple Data Types: Built-In and User- Defined.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Chapter 10: Records (structs)
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Chapter 10 Simple Data Types: Built-In and User- Defined.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems.
1 Lecture 19 Structs HW 5 has been posted. 2 C++ structs l Syntax:Example: l Think of a struct as a way to combine heterogeneous data values together.
Chapter 10 Simple Data Types: Built-In and User-Defined.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Today’s Material Aggregate Data Types: Structures and Unions
1 Chapter 12-2 Arrays Dale/Weems. 2 Using Arrays as Arguments to Functions Generally, functions that work with arrays require 2 items of information n.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Structures (aka records, structs) Textbook Chapter 11 sections:
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
1 Structural Types, Data Abstractions and Classes.
+ 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.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
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.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
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.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
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.
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 Chapter 10 & 11 enum & Structured Types Dale/Weems.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
1 Structured Types, Data Abstraction and Classes.
Chapter 10 Simple Data Types: Built-In and User-Defined
Basics (Variables, Assignments, I/O)
11 Chapter Structured Data
Dale/Weems/Headington
Chapter 10 Simple Data Types: Built-In and User-Defined
Heterogeneous aggregate datatypes
Basics (Variables, Assignments, I/O)
CS148 Introduction to Programming II
Structures Structured Data types Data abstraction structs ---
Structures, Unions, and Enumerations
Programming Fundamental
Presentation transcript:

cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT, unlike arrays, members also have names - field names, and may be different types; member

cosc237/structures2 Declaration 1. define template - form of the structure; struct name (or tag) { type member1; type member2; … };// no space reserved 2. Declare structure variable structname varname;/* allocates space */

cosc237/structures3 Example: struct Employee// structure tag { int idNumber; char name[30]; float salary; }; essentially creates a new variable type Employee emp1; Employee newEmp;

cosc237/structures4 Struct cont’d size determined by sum of all individual members ; required,tag is optional - anonymous struct { int idNumber; char name[30]; float salary; } newEmp;

cosc237/structures5 struct AnimalType // declares a struct data type, does not allocate memory { // struct members long id; char name[20]; char genus[10]; char species[10]; char country[15]; int age; float weight; }; // declare variables of AnimalType AnimalType animal1; AnimalType animal2;

cosc237/structures6 templates struct point { int x; //x-coordinate int y; //y-coordinate }; { int idNumber; char LName[30]; char FName[15]; float salary; };

cosc237/structures7 Scope of member name Member name is local to struct struct Employee// structure tag { int idNumber; char name[30]; float salary; }; struct Student { char ssNum[9]; char name[30]; }

cosc237/structures8 Declaration for structure variables: allocates space Example: 1. point point1; 2. Employee emp1; Employee emp2;

cosc237/structures9 Accessing individual members dot (.) operator is the member selection operator. structureName.memberName memberName not unique it is possible for members of different struct types to have the same identifiers. Also a non-struct variable may have the same identifier as a structure member. Examples: thisAnimal.weight, point1.x, point1.y, point2.x, point2.y; emp1.idNumber = 111; emp2.salary = 42000;

cosc237/structures10 Initialization structure variables can be initialized when they are declared: Employee newEmp = {999, "Ford", "John", 50000}; point point1 = {5, 10}; //x = 5 y = 10

cosc237/structures11 Reference animal1.age = 18; animal1.age++; animal2.id = ; cin >> animal1.weight; animal2.name = "giant panda"; animal2.genus[0] = toupper(animal2.genus[0]); newEmp.salary = newEmp.salary ; sizeof newEmp or sizeof (Employee)

cosc237/structures12 Strings in structures newEmp.name - “George” newEmp.name[0] - ‘G’ newEmp.name[1] - ‘e’

cosc237/structures13 Assignment one struct variable can be assigned to another, if both variables are of the same type –Employee emp1,emp2; –emp1 = emp2; Type compatibility - only if their types are identical or are renamings of the same type typedef Employee Worker;

cosc237/structures14 Operations valid on an entire struct type variable: Assignment operation: one struct variable can be assigned to another struct variable if both variables are of the same type. Example: –Employee emp1, emp2; emp1 = emp2; //copies the entire structure, member by member

cosc237/structures15 Telling Functions about Structures Structures can be passed as function arguments and can be returned as function return values. It's wasteful to pass large structures to a function that uses only one or two members of the structure. Can be passed by value or ref using same method as int, float, etc.

cosc237/structures16 Invalid Structure Operations (i.e.structure as a whole unit) I/O: cannot print or input structures as a whole cout >> newEmp;// invalid cout >> emp1.idNumber; Comparison: cannot compare entire structures if (emp1 < emp2) // invalid if (emp1.idNumber < emp2.idNumber)… // valid Arithmetic: cannot do arithmetic on entire structures newEmp = emp1 + emp2; // invalid newEmp.salary = emp1.salary + emp2.salary;// valid

cosc237/structures17 Examples of valid operations animal1 = animal2; // assignment, same type WriteOut(animal2); // value parameter ChangeAge(animal2); // reference parameter animal1 = GetAnimalData( ); // return value of function void ChangeAge ( AnimalType& thisAnimal) // struct as function argument { thisAnimal.age++; } AnimalType GetAnimalData ( ) // struct returned as function return value // Obtains all information about an animal from keyboard { AnimalType thisAnimal; char response; do { // have user enter all members until they are correct... } while (response != 'Y' ); return thisAnimal; }

cosc237/structures18 Hierarchical or nested structures struct DateType { int month; // Assume int day; // Assume int year; // Assume }; struct StatisticsType { float failRate; DateType lastServiced; // DateType is a struct type int downDays; }; struct MachineRec { int idNumber; char description[30]; StatisticsType history; // StatisticsType is a struct type DateType purchaseDate; // DateType is a struct type float cost; }; MachineRec machine; //variable of type MachineRec cout << machine.history.lastServiced.year; // may be 2001

cosc237/structures19 Example struct point { int x; //x-coordinate int y; //y-coordinate }; struct line { point start; // start point point end; // end point }; line diagonal; // variable of type line cout << diagonal.start.x; //the x-coord of the start point

cosc237/structures20 In general, structs are worthwhile only when used in combination with arrays (and, in later chapters, with pointers). Arrays and structs may be combined in various ways to form complex data structures. We can have arrays of structs, structs that contain arrays, arrays of structs with array components, and so on.

cosc237/structures21 Array of Structures Declaration const int NUM_COMP_EMP = 20; Employee compDept[NUM_COMP_EMP]; reference: arrayName[index].someMember compDept[0].name compDept[1].idNnumber not compDept.salary[0] compDept[2].name[3]

cosc237/structures22 Example struct point { int x; //x-coordinate int y; //y-coordinate }; point points[10]; //array variable of type point

cosc237/structures23 Example /* print the names of the employees in the computer department */ for (i = 0; i < NUM_COMP_EMP; i++) cout << compDept[i].name << endl;

cosc237/structures24 Unions device that lets you store different data types in the same memory space when several variables of different names refer to the same storage space; compiler reserves memory large enough to hold the largest data type

cosc237/structures25 Declaration: (similar to structures) union MixedArray { char name[10]; int age; float salary; double census; }; Define union variables: MixedArray p0; MixedArray save[10];

cosc237/structures26 Exa. union Holdem { int digit; double bigfl; char letter; } fit; fit - sizeof double fit.digit = 23;/* 23 is stored in fit,4 bytes used */ fit.bigfl = 2.0;/* 23 cleared, 2.0 stored; 8 bytes used fit.letter = 'h'; /* 2.0 cleared, h stored, 1 byte used */

cosc237/structures27 Anonymous Unions union { int someInt; float someFloat; }; member names are not local to union dot notation not used someInt = 67; used with Variant Records

cosc237/structures28 Variant Records Struct consisting of type field and one or more anonymous unions the member the anonymous union is called variant avoid nesting variant records within variant records always use a type field