Structs H&K Chapter 11 Instructor – Gokcen Cilingir Cpt S 121 (July 18, 2011) Washington State University.

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Advertisements

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.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.
(10-1) OOP: Inheritance in C++ D & D Chapter 20 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
Recursion H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Functions (II) H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 24, 2011) Washington State University.
Strings H&K Chapter 9 Instructor – Gokcen Cilingir
C Language Elements (II) H&K Chapter 2 Instructor – Gokcen Cilingir Cpt S 121 (June 22, 2011) Washington State University.
Strings (II) H&K Chapter 9 Instructor – Gokcen Cilingir Cpt S 121 (July 20, 2011) Washington State University.
Functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 23, 2011) Washington State University.
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Iteration in C H&K Chapter 5 Instructor – Gokcen Cilingir Cpt S 121 (July 1, 2011) Washington State University.
Selection structures – logical expressions and if statements H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 28, 2011) Washington State University.
Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
Selection structures in C (II) H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 30, 2011) Washington State University.
File processing with functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 27, 2011) Washington State University.
Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Chapter 11 Structure and Union Types Instructor: Alkar / Demirer.
1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions.
Chapter 11 Structure and Union Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;
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.
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
(4-2) Selection Structures in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 16, 2015) Washington State University.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter
1 STRUCTURES AND POINTERS. 2 A VARIABLE OF THIS COMPOSITE TYPE CAN HAVE MORE THAN ONE VALUE, GROUPED TOGETHER TO DESCRIBE AN ENTITY. THE COMPONENTS OF.
(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.
(1 - 1) Introduction to C Data Structures & Abstract Data Types Instructor - Andrew S. O’Fallon CptS 122 (August 26, 2015) Washington State University.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
(5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 21, 2015) Washington State University.
(4-3) Selection Structures II in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 18, 2015) Washington State University.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
(3-2) File Processing with Functions Instructor - Andrew S. O’Fallon CptS 121 (September 11, 2015) Washington State University.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
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.
Module 1: Array ITEI222 - Advance Programming Language.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
chap11 Chapter 11 Structure and Union Types.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(6-2) Iteration in C II H&K Chapter 5 Instructor - Andrew S. O’Fallon CptS 121 (February 19, 2016) Washington State University.
Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University.
Classes: A Deeper Look D & D Chapter 9 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Structured Data Objects (Structs). array Recall our definition of an array: fixedcontiguousall of the same data type an array is a fixed number of contiguous.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Programming application CC213 Week 08 – Struct & Unions– Chapter 10.
(1-3) Basics of a Linked List I Instructor - Andrew S. O’Fallon CptS 122 (June 9, 2016) Washington State University.
LINKED LISTS.
(2-1) Data Structures & The Basics of a Linked List I
(2-1) Data Structures & The Basics of a Linked List I
Structure and Union Types
(1-1) C Review: Pointers, Arrays, Strings, & Structs
(1 - 2) Introduction to C Data Structures & Abstract Data Types
Chapter 10 C Structures and Unions
Chapter 11 Structure and Union Types.
(4 – 2) Introduction to Classes in C++
Presentation transcript:

Structs H&K Chapter 11 Instructor – Gokcen Cilingir Cpt S 121 (July 18, 2011) Washington State University

C. Hundhausen, A. O’Fallon, G. Cilingir2 struct Type (1) C supports another kind of user-defined type: the struct struct s are a way to combine multiple variables into a single "package" (this is called "encapsulation") struct s may be an alternative to ◦ using output parameters to return more than one value from a function ◦ using parallel arrays or multidimensional arrays to store associated information

C. Hundhausen, A. O’Fallon3 struct Type (2) typedef enum { freshman, sophomore, junior, senior } class_t; /* class standing */ typedef enum { anthropology, biology, chemistry, english, compsci, polisci, psychology, physics, engineering, sociology } major_t; /* representative majors */ typedef struct { int id_number; class_t class_standing; /* see above */ major_t major; /* see above */ double gpa; int credits_taken; } student_t; Suppose, for example, that we want to create a database of students in a course. We could define a student struct as follows:

C. Hundhausen, A. O’Fallon4 struct Type (3) We can then define some students: student_t student1, student2; student1.id_num = ; student1.class_standing = freshman; student1.major = anthropology; student1.gpa = 3.5; student1.credits_taken = 15; student2.id_num = ; student2.class_standing = senior; student2.major = biology; studnet2.gpa = 3.2; student2.credits_taken = 100; Notice how we use the "." (selection) operator to access the "fields" of the struct

C. Hundhausen, A. O’Fallon5 struct Type (4) We can easily make a copy of a whole structure simply by using the assignment operator: /* each field is copied to the corresponding field in student3 */ student_t student3 = student1;

C. Hundhausen, A. O’Fallon6 struct Type (5) We can also return a struct as a function result: student_t read_student() { student_t student; int temp_class, temp_major; printf("Please enter ID number of student: "); scanf("%d",&student.id_num); printf("Please enter class standing (0 = fr,\n"); printf("1 = so, 2 = ju, 3 = se): "); scanf("%d",&temp_class); student.class = (class_t)temp_class; printf("Please enter major (0 = anthro.,\n"); printf("1 = biol., 2 = chem., …, 8 = soc.: "); scanf("%d",&temp_major); student.major = (major_t)temp_major; printf("Please enter gpa: "); scanf("%lf",&student.gpa); printf("Please enter credits taken: "); scanf("%d",&student.credits_taken); return student; }

C. Hundhausen, A. O’Fallon7 struct Type (6) Here's how we could use the previous function: int main(void) { student_t student1, student2; student1 = read_student(); student2 = read_student(); print_student(student1); /* assume print_student is defined */ print_student(student2); return(1); }

C. Hundhausen, A. O’Fallon8 struct Type (7) We can rewrite the previous function so that it fills in an output parameter: void read_student(student_t *student) { int temp_class, temp_major; printf("Please enter ID number of student: "); scanf("%d",&((*student).id_num)); printf("Please enter class standing (0 = fr,\n"); printf("1 = so, 2 = ju, 3 = se): "); scanf("%d",&temp_class); (*student).class = (class_t)temp_class; printf("Please enter major (0 = anthro.,\n"); printf("1 = biol., 2 = chem., …, 8 = soc.: "); scanf("%d",&temp_major); (*student).major = (major_t)temp_major; printf("Please enter gpa: "); scanf("%lf",&((*student).gpa)); printf("Please enter credits taken: "); scanf("%d",&((*student).credits_taken)); }

C. Hundhausen, A. O’Fallon9 struct Type (8) Here's how we could use the previous function: int main(void) { student_t student1, student2; read_student(&student1); read_student(&student2); print_student(student1); /* assume print_student is defined */ print_student(student2); return(1); }

C. Hundhausen, A. O’Fallon10 struct Type (9) C provides the -> (component selection) operator as a means of accessing struct fields. This provides a nice alternative to the * operator: void read_student(student_t *student) { int temp_class, temp_major; printf("Please enter ID number of student: "); scanf("%d",&(student->id_num)); printf("Please enter class standing (0 = fr,\n"); printf("1 = so, 2 = ju, 3 = se): "); scanf("%d",&temp_class); student->class = (class_t)temp_class; printf("Please enter major (0 = anthro.,\n"); printf("1 = biol., 2 = chem., …, 8 = soc.: "); scanf("%d",&temp_major); student->.major = (major_t)temp_major; printf("Please enter gpa: "); scanf("%lf",&(student->gpa)); printf("Please enter credits taken: "); scanf("%d",&(student->credits_taken)); }

C. Hundhausen, A. O’Fallon11 struct Type (10) Notes ◦ struct types are most often used in applications that work with databases  student records  employee records  planet records ◦ Often, we define databases as arrays of structs ◦ For now, just understand that a struct is a way to encapsulate multiple variables in a single "package"

12 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (6 th Ed.), Addison- Wesley, 2010 P.J. Deitel & H.M. Deitel, C How to Program (5 th Ed.), Pearson Education, Inc., 2007.