© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Programming application CC213 Week 08 – Struct & Unions– Chapter 10.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
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 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
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];
CS 201 Structure & Union Debzani Deb.
Chapter 11 Structure and Union Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
CS 201 Functions Debzani Deb.
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;
Structure and Union Types 程式設計 潘仁義 CCU COMM. Structure Type Definition struct structured data objects, can be defined by users #define STRSIZ 10 typedef.
TDBA66, Lecture Ch. 11, vt-03 1 Abstraction Prucedural abstraction moduralize by hiding the details in a function() Ex.functions in get_params(…) display_match(…)
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Programming in C Structs and Unions. No Classes in C Because C is not an OOP language, there is no way to combine data and code into a single entity.Because.
How to start Visual Studio 2008 or 2010 (command-line program)
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 ICS103 Programming in C Lecture 8: Functions I.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
chap11 Chapter 11 Structure and Union Types.
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.
A Sample Program #include using namespace std; int main(void) { cout
Problem Solving and Program Design in C Chap. 11 Structure and Union Types Chow-Sing Lin.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction We will study how to broaden the modeling facilities of C by defining our own data types that represent structured collections of data pertaining.
Perimeter, Area, and Circumference
Pointers and Dynamic Arrays
Structures, Unions, Enumerations
CS1001 Programing Fundamental Lecture 12 Strings and Structured Type
CSE 220 – C Programming C Fundamentals.
Computer Science 210 Computer Organization
© 2016 Pearson Education, Ltd. All rights reserved.
CS1010 Programming Methodology
© 2016 Pearson Education, Ltd. All rights reserved.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Functions Separate Compilation
CS1010 Programming Methodology
This technique is Called “Divide and Conquer”.
Functions in C Mrs. Chitra M. Gaikwad.
Tejalal Choudhary “C Programming from Scratch” Pointers
Computer Science 210 Computer Organization
Programming in C #4 Structs and Unions.
C Structures, Unions, Bit Manipulations and Enumerations
Simple Data Types and Function Calls
Structure and Union Types
Structure and Union Types
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
1.7 Introduction to Perimeter, Circumference, & Area
1.5 Using Formulas in Geometry
READING AND PRINTING MATRICES (with functions)
Variables in C Topics Naming Variables Declaring Variables
Chapter 10 C Structures and Unions
Chapter 11 Structure and Union Types.
Computer Science II CS132/601* Lecture #C-3.
Dynamic Data Structures
Presentation transcript:

© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Programming application CC213 Week 08 – Struct & Unions– Chapter 10

1-2 © 2012 Pearson Addison-Wesley. All rights reserved. Outline User-defined structure types Structure type data as input and output parameter Functions whose result values are structured Union Type 2

1-3 © 2012 Pearson Addison-Wesley. All rights reserved. User-defined structure types Syntax: Example 1 typedef statement allocates no memory. A later declaration creates a variable built on the template of the type definition, will create contagious area in memory for all members of the struct. 3 typedef struct{ type1 name1; type2 name2; }struct type; typedef struct{ /*complex number structure*/ double real; double imaginary; }complex_t;

1-4 © 2012 Pearson Addison-Wesley. All rights reserved. Example 2 4 typedef struct{ char name[10]; double diameter; int moons; double orbit_time, rotation_time; }planet_t; int main (void) { int x; planet_t current_planet; planet_t previous_planet; planet_t blank_planet = { " ", 0, 0, 0, 0}; float y; }

1-5 © 2012 Pearson Addison-Wesley. All rights reserved. Manipulating a struct 1.Manipulating individual component –Ex: 2.Manipulating whole structure –previous_planet = current_planet; 5 strcpy(current_planet.name, "Jupiter"); current_planet.diameter = ; current_planet.moons = 16; current_planet.orbit_time = 11.9; current_planet.rotation_time = 9.925;

1-6 © 2012 Pearson Addison-Wesley. All rights reserved. As an input and output parameters The following function prints the contents of the struct 6 void print_planet(planet_t pa) { printf("%s\n", pa.name); printf("the diameter is: %f km\n", pa.diameter); printf("number of moons : %d \n", pa.moons); printf("orbit time is : %f years\n", pa.orbit_time); } main() { int x; print_planet(current_planet); }

1-7 © 2012 Pearson Addison-Wesley. All rights reserved. Passing a Struct as an Output Argument 7 /* * Fills a type planet_t structure with input data. Integer returned as * function result is success/failure/EOF indicator. * 1 => successful input of one planet * 0 => error encountered * EOF => insufficient data before end of file * In case of error or EOF, value of type planet_t output argument is * undefined. */ int scan_planet(planet_t *plnp) { /* output - address of planet_t structure to fill */ int result; result = scanf("%s%lf%d%lf%lf", (*plnp).name, if (result == 5) result = 1; else if (result != EOF) result = 0; return (result); }

1-8 © 2012 Pearson Addison-Wesley. All rights reserved. Pass by reference memory content 8

1-9 © 2012 Pearson Addison-Wesley. All rights reserved. Functions whose result values are structured 9 planet_t get_planet(void) { planet_t planet; scanf("%s%lf%d%lf%lf", planet.name, &planet.diameter, &planet.moons, &planet.orbit_time, &planet.rotation_time); return(planet); } main() { planet_t current_planet; current_planet = get_planet(); }

1-10 © 2012 Pearson Addison-Wesley. All rights reserved. Figure An Array of Structures 1-10

1-11 © 2012 Pearson Addison-Wesley. All rights reserved. Functions whose result values are structured 11 #define MAX_STU 50 #define NUM_PTS 10 typedef struct { int id; double gpa; } student_t; typedef struct { double x, y; } point_t; main() { student_t stulist[MAX_STU]; point_t polygon[NUM_PTS]; for (i = 0; i < MAX_STU; ++i) scan_student(&stulist[i]); for (i = 0; i < MAX_STU; ++i) printf("%d\n", stulist[i].id); } Implement “scan_student” function

1-12 © 2012 Pearson Addison-Wesley. All rights reserved. Union Unions are structured types in which some components vary depending on the value of another component. Example: to compute are and circumference of a geometric shape, we will need different data based on the shape processed: radius for a circle, side length for a square, width and height for a rectangle 12

1-13 © 2012 Pearson Addison-Wesley. All rights reserved. Example 1 This variable hair_data does not contain both wears_wig and color components. Rather, it has either a wears_wig component referenced by hair_data.wears_ wig or a color component referenced by hair_data.color. When memory is allocated for hair_data, the amount of memory is determined by the largest component of the union. 13 typedef union { int wears_wig; char color[20]; } hair_t; hair_t hair_data; typedef struct { int bald; hair_t h; } hair_info_t;

1-14 © 2012 Pearson Addison-Wesley. All rights reserved. Function That Displays a Structure with a Union Type Component 14 Void print_hair_info(hair_info_t hair) { /* input – structure to display */ if (hair.bald) { printf("Subject is bald"); if (hair.h.wears_wig) printf(", but wears a wig.\n"); else printf(" and does not wear a wig.\n"); } else { printf("Subject's hair color is %s.\n", hair.h.color); }

1-15 © 2012 Pearson Addison-Wesley. All rights reserved. Two Interpretations of Parameter hair 15

1-16 © 2012 Pearson Addison-Wesley. All rights reserved. Example 2 16 /* Types defining the components needed to represent each shape. */ typedef struct { double area, circumference, radius; } circle_t; typedef struct { double area, perimeter, width, height; } rectangle_t; typedef struct { double area, perimeter, side; } square_t; /* Type of a structure that can be interpreted a different way for each shape */ typedef union { circle_t circle; rectangle_t rectangle; square_t square; } figure_data_t; /* Type containing a structure with multiple interpretations along with a component whose value indicates the current valid interpretation */ typedef struct { char shape; figure_data_t fig; } figure_t;

1-17 © 2012 Pearson Addison-Wesley. All rights reserved. Exercise: Implement incomplete functions 17 figure_t get_figure_dimensions(void); figure_t compute_area(figure_t object); figure_t compute_perim(figure_t object); void print_figure(figure_t object); int main(void) { figure_t onefig; printf("Area and Perimeter Computation Program\n"); for (onefig = get_figure_dimensions(); onefig.shape != 'Q'; onefig = get_figure_dimensions()) { onefig = compute_area(onefig); onefig = compute_perim(onefig); print_figure(onefig); } return (0); }