Heterogeneous Structures 4 Collection of values of possibly differing types 4 Name the collection; name the components 4 Example: student record harveyC.

Slides:



Advertisements
Similar presentations
Programming in C Chapter 10 Structures and Unions
Advertisements

1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
Structures Often we want to be able to manipulate logical entities as a whole For example, complex numbers, dates, student records, etc Each of these must.
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 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structures Spring 2013Programming and Data Structure1.
 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.
Structures A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
C and Data Structures Baojian Hua
M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.
C language issues CSC 172 SPRING 2002 EXTRA LECTURE.
ESC101: Introduction to Computing Structures. Motivation Till now, we have used data types int, float, char, arrays (1D, 2D,...) and pointers. What if.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
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.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
Structures ANSI-C. Review: Data Structures Functions give us a way to organize programs. Data structures are needed to organize data, especially: –1.
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.
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
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.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
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 Structs. 2 Defining a Structure Often need to keep track of several pieces of information about a given thing. Example: Box We know its length width.
M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
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 Lecture06: Complex Types 4/18/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 5.
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.
Structures 142 S -1 What is a structure? It is a collection of possibly different types Name the collection Name the components For example: a student.
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.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
CE-2810 Dr. Mark L. Hornick 1 “Classes” in C. CS-280 Dr. Mark L. Hornick 2 A struct is a complex datatype that can consist of Primitive datatypes Ints,
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
P-1 5/18/98 CSE / ENGR 142 Programming I Arrays of Structures © 1998 UW CSE.
C Structures and Memory Allocation
Parallel Arrays? ANSI-C.
Introduction to Programming Using C
Homework / Exam Continuing K&R Chapter 6 Exam 2 after next class
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Programming in C Pointer Basics.
Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct. A struct is heterogeneous.
הגדרת משתנים יום שלישי 27 נובמבר 2018
Array of Structures A structure holds only one record But a record
Pointers & Functions.
Programming in C Pointer Basics.
CS148 Introduction to Programming II
Programming in C Pointer Basics.
CS111 Computer Programming
Structures CSE 2031 Fall February 2019.
Programming Structures.
Structures CSE 2031 Fall April 2019.
Structures CSE 2031 Fall May 2019.
Pointers & Functions.
Programming in C Pointer Basics.
Programming Structures.
Structures EECS July 2019.
C Structures and Memory Allocation
Presentation transcript:

Heterogeneous Structures 4 Collection of values of possibly differing types 4 Name the collection; name the components 4 Example: student record harveyC expressions: name “Harvey S.Harvey.hw is 87 id harvey.name is “Harvey S.” hw 87 exams 742*harvey.exams is 148 grade 3.1

Defining structs #define MAX_NAME 40 typedef struct { /* typedefs go at top of prog */ charname [MAX_NAME + 1]; int id; int hw, exams; double grade; } student_record; –Defines a new data type called student_record. Does not declare a variable!

Terminology 4 A “struct” is sometimes called a “record” or “structure” 4 Its “components” are sometimes called “fields” or “members” 4 Srtucts lead to classes in C++ and Java, and become the basis for object-oriented programming

Declaring struct Variables … /* typedefs go at top of program */ … int i1; /* int decls and initializers */ int count=0; /* nothing new */ char c1[5]; /* array decls */ student_record s1; student_record harvey; /* student_record is a type; s1 and harvey are variables */

Struct Initializers … /* typedefs go at top of program */ int i1; /* int decls and initializers */ int count=0; /* nothing new */ char c1[5]=“lamb”; /* string initializer */ student_record harvey = {“Harvey S.”, , 87, 74, 4, 1};

Using Components of struct Vars S1.hw = 90; s1.exam = 80; s1.grade=(double)(s1.hw + s1.exams)/50.0; printf(“%d: %f”,harvey.id, harvey.grade); scanf(“%d”, &s1.id); i1 = strlen(harvey.name);

Assigning Whole structs S1 = harvey; equivalent to: strncpy(s1.name,harvey.name,MAX_NAME+1); s1.id = harvey.id; s1.hw = harvey.hw; s1.exams = harvey.exams; s1.grade = harvey.grade;

Things You Can and Can’t Do 4 You can –use = to assign whole struct variables 4 You can –have a struct as a function return type 4 You can’t –use == to directly compare struct variables; can write your own function 4 You can’t –directly scanf or printf structs; need a function

Why use structs? 4 Collect together values that are treated as a unit (for readability, maintainability)typedef struct { int dollars, cents;int hours, minutes; } money;double seconds; } time; 4 Functions can return structs –another way to have multiple return values 4 Files and databases: collection of structs (e.g. 200 student records)

Midpoint Example /* Given 2 endpoints of a line, “return” coordinates of midpoint */ void setmidpoint( double x1, double y1, /* 1st endpoint */ double x2, double y2, /* 2nd endpoint */ double *midxp, double *midyp) /* endpoint */ { *midxp = (x1 + x2)/2.0; *midyp = (y1 + y2)/2.0; } double ax, ay, bx, by, mx, my;... set_midpoint(ax, ay, bx, by, &mx, &my);

Points as structs Typedef struct { double x, y; } point; … point a = {0.0, 0.0}, b = {5.0, 10.0}; point m; m.x = (a.x + b.x)/2.0; m.y = (a.y + b.y)/2.0;

Midpoint function via structs point midpoint(point pt1, point pt2) { point mid; mid.x = (pt1.x + pt2.x)/2.0; mid.y = (pt1.y + pt2.y)/2.0; return(mid); } point a = {0.0, 0.0}, b = {5.0, 10.0}, m; m = midpoint(a, b); /* struct assignment */ Structs passed by value (copied)

Testing Equality of structs if (pt1 == pt2) (…) /* Won’t work */ int points_equal(point pt1, point pt2) { return (pt1.x == pt2.x && pt1.y == pt2.y); } if (points_equal(pt1,pt2)) {…} /* OK */

Do-it-yourself struct I/O Void (print_point p) { printf(“(%f, %f)”, p.x, p.y); } void scan_point(point *pp) { point temp; scanf(“%lf %lf”, &temp.x, &temp.y); *pp = temp; } point a; scan_point(&a); print_point(a); pp: temp: x: y: a: x: y:

Alternative scan_point void scan_point(point *pp) { scanf(“%lf %lf”, &(*pp).x, &(*pp).y); } point a; scan_point(&a); print_point(a); pp: a: x: y:

Pointers and structs expressiontype pppoint * *pppoint (*pp).xdouble &(*pp).xdouble * pp a a.x a.y &(*pp).x&(*pp).y