User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;

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

C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
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.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
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.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Chapter 11 Structure and Union Types Instructor: Alkar / Demirer.
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.
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { char name[10];
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
CS 201 Structure & Union Debzani Deb.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
Chapter 11 Structure and Union Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11. The Struct Data Type.
Chapter 10: Records (structs)
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
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.
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.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Structures (aka records, structs) Textbook Chapter 11 sections:
Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
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.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
chap11 Chapter 11 Structure and Union Types.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Problem Solving and Program Design in C Chap. 11 Structure and Union Types Chow-Sing Lin.
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.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
1 Chapter 10 & 11 enum & Structured Types Dale/Weems.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Programming application CC213 Week 08 – Struct & Unions– Chapter 10.
Chapter 7: Introduction to Classes and Objects
Student Book An Introduction
Data Types – Structures
Heterogeneous aggregate datatypes
Structure and Union Types
Structure (i.e. struct) An structure creates a user defined data type
Structures Structured Data types Data abstraction structs ---
Chapter 10 C Structures and Unions
Chapter 11 Structure and Union Types.
Structures, Unions, and Enumerations
Programming Fundamental
Presentation transcript:

User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name; int age; char sex; double grade_avg; } student_t; typedef statement itself allocates no memory. A variable declaration is required to allocate storage space for a structured data object. E.g. student_t one_student; This type definition is a template that describes the format of a student structure and the name and type of each component.

How to reference a member of a Structure We can reference a component of a structure by using the direct component selection operator, which is a period. one_student. name one_student. age one_student. sex one_student. grade_avg A name chosen for a component of one structure may be the same as the name of a component of another structure.

Assigning Values to Components of variable one_student one_student. Name= “David”; one_student. age = 22; one_student. sex = ‘M’; one_student. grade_avg = 90.0; variable one_student, a structure of type student_t D a v i d \0 ? ? ? ? 22 M 90.0.name.age.sex.grade_avg

Hierarchical Structure Hierarchical structure => a structure containing components that are structures (arrays or structs). typedef struct { char name[10]; int age; char sex; double grade_avg; } student_t; Note: To avoid confusion, we choose user_defined type names that use lowercase letters and end in the suffix_t.

Hierarchical Structure typedef struct { char place[20]; long_lat_t longitude, latitude; }location_t ; location_t resort; typedef struct { int degrees, minutes; char direction; } long_lat_t; F i j i \0 ? ? ?…… S E variable resort Reference Data Type Value resort. latitude long_lat_t ‘S’ resort. place ? ? resort. longitude. direction ? ?.place.longitude.latitude

Manipulating Whole Structure The name of a structure type variable used with no component selection operator refers to the entire structure. A new copy of a structure’s value can be made by simply assigning one structure to another. Example: student_t student_one, student_two; student_one = student_two;

Arithmetic Operations and Comparisons Aggregate arithmetic operations and comparisons are not allowed. Examples: student_t stu1,stu2; stu1 = stu1 * stu2; // Not allowed if (stu1 < stu2) // Not allowed

Aggregate I/O is not permitted cin >> stu1; // Not allowed We must input or output a struct variable one member at a time. Examples:  cin >> stu1. name;  cin >> stu1. Age;

Structure Type Data as Input Parameter void print_student(student_t stu) { cout << “Name : “<< stu. name); cout << “Age: “ << stu. age); cout << “Sex: “<< stu. sex); cout << “Grade Avg: “<< stu. grade_avg; } Call statement: print_student( one_student ); Input parameter

Function Comparing Two Structured Values for Equality int student_equal (student_t stu_1, // takes two students student_t stu_2) // as input argument { return ( stu_1. name = = stu_2. name && stu_1. age == stu_2. age && stu_1. sex == stu_2. sex && stu_1. grade_avg == stu_2. grade_avg); } // returns 1 if all components match, otherwise returns 0.

Function with a Structured Output Argument void student_info (student_t *stup) // address of student_t { cout << "Enter name"<< endl; cin >> (*stup). name; cout << "Enter age"<< endl; cin >> (*stup). age; cout << (*stup). name << endl; cout << (*stup). age << endl; }

Indirect Component Selection Operator int scan_student (student_t *stup) // address of student_t { cout << "Enter name"<< endl; cin >> stup -> name; cout << "Enter age"<< endl; cin >> stup -> age; cout name << endl; cout age << endl; }

Function that Returns a Structured Result Type student_t get_student (void) { student_t stu; cin>> stu. name; cin >> stu. age; cin >> stu. sex; cin >> stu. grade_avg; return (stu); }

Array of Structures const int MAX_STU = 50; typedef struct { int id; double gpa; } student_t; student-_t stulist[MAX_STU]; Array stulist stulist[0] stulist[1] stulist[2] stulist[3] stulist[4] stulist[5] stulist[0]. id.id.gpa

Union Types typedef union { int wears_wig; // if the person is bald, we will // notice if he wears a wig char color[20]; // if the person has hair, we will // record hair color } hair_t; hair_t hair_data; // creates a variable built on the // template of the type definition hair_data does not contain both wears_wig and color. It has either wear_wig or color. The amount of memory is determined by the largest component of the union.

Union Type typedef struct { int bald; // component that indicates which // interpretation of union is correct at present hair_t h; } hair_info_t; If the person is bald, the wear_wig interpretation of component h is valid. For nonbald person, the color interpretation is valid and represents the color of the person’s hair.

Function that Display a Structure with Union Type Component void print_hair_info(hair_info_t hair) { if (hair.bald) { cout << “Subject is bald”; if (hair.h.wears_wig) cout << “, but wears a wig.” <<endl; else cout<<“and does not wear a wig.”<<endl; } else cout<<“Subject’s hair color is : ”<< hair.h.color<<endl; }

Union A union is a derived data type- like a structure- whose members share the same storage space. For different situations in a program, some variables may not be relevant, but other variables are relevant. So a union shares the space instead of wasting storage on variables that are not being used. The members of a union can be of any type. The number of bytes used to store a union must be at least enough to hold the largest member. In most cases, unions contain two or more data types. Only one member, and thus one data type, can be referenced at a time. It is the programmer’s responsibility to ensure that the data in a union is referenced with the proper data type.