1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.

Slides:



Advertisements
Similar presentations
C Programming Lecture 23 Enumeration Types Structures.
Advertisements

CSC141- Introduction to Computer Programming
C Language.
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.
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 in C.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
1 Types The type of a variable represents a set of values that may be assigned to the variable. For example, an integer variable is one that may take the.
C and Data Structures Baojian Hua
Chapter 11 Structure and Union Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney.
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #6 Structures,
Chapter 16: Structures, Unions, and Enumerations Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 16 Structures, Unions, and Enumerations.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
User Defined Data Types - updated Chapter 10: User Defined Data Types Objectives: In this chapter you will learn about, Introduction Declaring.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.Semantic © In this session we will.
Array, Structure and Union
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
C Programming Lecture 11 : Structure. Data Type struct union enum typedef.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Enumeration.
Structure Types Suppose that a program needs to declare several structure variables with identical members. We need a name that represents a type of structure,
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
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.
1 Lecture06: Complex Types 4/18/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
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.
chap11 Chapter 11 Structure and Union Types.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
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.
LESSON 06.
Exam 3 Coverage (everything up to strings) Two Sections Pointers
Structures, Unions, Enumerations
Structures, Unions, and Enumerations
Structures, Unions, and Enumerations
typedef typedef int Index; typedef char Letter; Index i; i = 17;
MON TUE WED THU
Structures, Unions, and Enumerations
Structures, Unions, and Enumerations
Structures.
Programming in C Pointer Basics.
Structures, Unions, and Enumerations
Programming in C Pointer Basics.
Chapter 1: Introduction to Data Structures(8M)
Sun Mon Tue Wed Thu Fri Sat
Programming Language C Language.
Structures In C Programming By Rajanikanth B.
Sun Mon Tue Wed Thu Fri Sat
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
EECE.2160 ECE Application Programming
Structures Chapter 4.
Structures, Unions, and Enumerations
CS100A Lect. 12, 8 Oct More About Arrays
Presentation transcript:

1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C

Administrative Items Assignment #10 on the course webpage r, long double, etc. 2

Complex Types Structure –User-defined combinations of other types Enumeration –An integer type whose values are named by the programmer Union –Same data, multiple interpretations 3

Structure A structure is a collection of variables of (different) types –Defined with the keyword struct Example struct student { int id; int year; char grade; }; void main() { struct student s; s.id = 10001; s.year = 2011; s.grade = 'A'; } 4 id year grade structure id year grade

Structure Syntax 5 To define a structure Definition struct structure_name { /* list of variable declarations */ }; To declare a variable of the new structure type Example void main() { struct structure_name variable_name; } The struct keyword is required in both places

Initializing Structure Variable #define NAME_LEN 20 struct part { int number; char name[NAME_LEN+1]; int on_hand; }; int main() { struct part part1 = {528, "Disk drive", 10}; struct part part2 = {.number=914,.name="Printer cable",.on_hand=5}; struct part part3; part3.number = 321; strcpy(part3.name, "Computer Display"); part3.on_hand = 8; } 6

Operations on Structure #define NAME_LEN 20 struct part { int number; char name[NAME_LEN+1]; int on_hand; }; int main() { struct part part1 = {528, "Disk drive", 10}; struct part part2 = {914, "Printer cable", 5}; struct part temp; temp = part1; part1 = part2; part2 = temp; printf("Part number: %d\n", part1.number); printf("Part name: %s\n", part1.name); printf("Quantiy on hand: %d\n", part1.on_hand); return 0; } 7

Defining Structure Types struct part { int number; char name[NAME_LEN+1]; int on_hand; }; part temp; /* wrong, part is just the structure name, * not a type */ struct part temp; /* okay */ 8

Defining Structure Types: typedef typedef struct { int number; char name[NAME_LEN+1]; int on_hand; } Part;/* now Part becomes the name of * a type. Use it in the same way * as other built-in types */ Part part1, part2; 9

Structures as Arguments struct part { int number; char name[NAME_LEN+1]; int on_hand; }; void print_part(struct part p) { printf("Part number: %d\n", p.number); printf("Part name: %s\n", p.name); printf("Quantiy on hand: %d\n", p.on_hand); } int main() { struct part part1 = {528, "Disk drive", 10}; print_part(part1); return 0; } 10

Structures as Return Values struct part { int number; char name[NAME_LEN+1]; int on_hand; }; struct part build_part(int number, char *name, int on_hand) { struct part p; p.number = number; strcpy(p.name, name); p.on_hand = on_hand; return p; } int main() { struct part part1; part1 = build_part(528, "Disk drive", 10); return 0; } 11

Nested Structures struct person_name { char first[NAME_LEN+1]; char middle_initial; char last[NAME_LEN+1]; }; struct student { struct person_name name; int id, age; char sex; }; 12

Array of Structures struct part { int number; char name[NAME_LEN+1]; int on_hand; }; int main() { struct part inventory[100]; } 13

In-Class Exercise 10.1 Maintain a parts database for a warehouse. the program is built around an array of structures, with each structure containing information, part number, and quantity – about one part. Our program will support the following operations: -void insert() : Ask the user to add information about a new part number, part name, and initial quantity on hand. -void print(): Print all the parts in a database. -void search(): Ask th user to enter a part number. If the part exists, print the name and quantity on hand. If not, print an error message. Your program ought to have the following input & output (next slide): 14

In-Class Exercise #1 Enter operation code: i Enter part number: 528 Enter part name: Disk drive Enter quantity on hand: 10 Enter operation code: s Enter part number: 528 Part name: Disk drive Quantity on hand: 10 Enter operation code: s Enter part number: 914 Part not found. Enter operation code: i Enter part number: 914 Enter part name: Printer cable Enter quantity on hand: 5 Enter operation code: p Part number Part Name Quantity on Hand 528Disk drive8 914Printer cable5 Enter operation code: q 15

Pointers to Structures typedef struct { char name[10000]; int id; } person; void print_person(person *p) { printf("%d %s\n", p->id, p->name); /* same as (*p).id and (*p).name */ } void main() { person a = {"Mike Smith", 1234}; print_person(&a); } 16

Union A union is like struct has many members, possible of different types. Computer allocates only enough space for the largest of its member. Example union { int i; double d; } u; u.i = 82; u.d = 74.8; // over-writing the value of i 17 union i d

Why Union? Save space struct catalog_item { int stock_number; double price; int item_type; /* books */ char title[TITLE_LEN+1]; char author[AUTHOR_LEN+1]; int num_pages; /* mugs & shirts */ char design[DESIGN_LEN+1]; int colors; int sizes; } struct catalog_item { int stock_number; double price; int item_type; union { struct { char title[TITLE_LEN+1]; char author[AUTHOR_LEN+1]; int num_pages; } book; /* books */ struct { char design[DESIGN_LEN+1]; } mug; /* mug */ struct { char design[DESIGN_LEN+1]; int colors; int sizes; } shirt; /* shirt */ } 18

In-Class Exercise 10.2 #include #define INT_KIND 0 #define DOUBLE_KIND 1 typedef struct { int kind; union { int i; double d; } u; } Number; void print_number(Number n) { /* fill your code here */ } int main() { Number n; n.kind = INT_KIND; n.u.i = 82; print_number(n); n.kind = DOUBLE_KIND; n.u.d = 82.56; print_number(n); } 19

Enumerations: basically an int enum days {mon, tue, wed, thu, fri, sat, sun}; /* You can name the values for 0, 1, 2, 3, 4, … * make the code easy to read * #define mon 0 * #define tue 1 *... * #define sun 6 */ enum days {mon = 3, tue = 8, wed, thu, fri, sat, sun}; /* Same as: * #define mon 3 * #define tue 8 * #define wed 9 *... * #define sun 13 */ 20

Enumerations enum days day; // Same as: int day; for (day = mon; day <= sun; ++day) { if (day == sun) { printf("Sun\n"); } else { printf("day = %d\n", day); } 21