Homework / Exam Continuing K&R Chapter 6 Exam 2 after next class

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Operator Overloading Fundamentals
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
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 6.1 Basics of Structures 6.2 Structures and Functions 6.3 Arrays of Structures 6.4 Pointers to Structures 6.5 Self-referential Structures + malloc +
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Heterogeneous Structures 4 Collection of values of possibly differing types 4 Name the collection; name the components 4 Example: student record harveyC.
Structures and UnionsCS-2301 B-term Structures and Unions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 6 : September 6.
C language issues CSC 172 SPRING 2002 EXTRA LECTURE.
Pointers Applications
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
1 6.1 Basics of Structures 6.2 Structures and Functions 6.3 Arrays of Structures 6.4 Pointers to Structures 6.5 Self-referential Structures + Linked List.
Homework Finishing up K&R Chapter 6 today Also, K&R 5.7 – 5.9 (skipped earlier)
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.
Homework Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next.
Data Types Declarations Expressions Data storage C++ Basics.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
1 Homework / Exam Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next Continue HW5.
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.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
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)
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
C Structures and Memory Allocation
Discussion on mp1.
Chapter 5, Pointers and Arrays
EGR 2261 Unit 11 Pointers and Dynamic Variables
Java Yingcai Xiao.
Data types Data types Basic types
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Visit for more Learning Resources
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Buy book Online -
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
CS1100 Computational Engineering
Structures, Unions, and Typedefs
Regrading Project 2 Exam 2 One week period TA: Huiyuan TA: Fardad
CS111 Computer Programming
Homework Finishing K&R Chapter 5 today Starting K&R Chapter 6 next
Homework Starting K&R Chapter 5 Good tutorial on pointers
Homework Continuing K&R Chapter 6.
Structures CSE 2031 Fall February 2019.
C++ Pointers and Strings
Pointers Pointers point to memory locations
Data Structures and Algorithms Introduction to Pointers
Structures CSE 2031 Fall April 2019.
Structures CSE 2031 Fall May 2019.
CS1100 Computational Engineering
Arrays and Pointers CSE 2031 Fall May 2019.
Structures EECS July 2019.
C Structures and Memory Allocation
C++ Pointers and Strings
User-defined data types
Arrays and Pointers CSE 2031 Fall July 2019.
Presentation transcript:

Homework / Exam Continuing K&R Chapter 6 Exam 2 after next class Open Book / Open Notes Up through end of K&R 6.4 plus MAKE

Review structs struct point { int x; int y; } pt; struct point pt1, pt2; struct point maxpt = {320, 200};

Review structs struct rect { struct point pt1; struct point pt2; };  Note: must have ; following } struct rect box;

Review structs box.pt1.x = 5; box.pt1.y = 10; box.pt2.x = 10; area = (box.pt2.x – box.pt1.x) * (box.pt2.y – box.pt1.y);

What can we do with a struct? Reference members box.pt2.x = box.pt1.x + width; Assign as a unit pt2 = pt1; Create a pointer to it struct point *ppt1; ppt1 = &pt1;

What can we do with a struct? Not legal to compare structs if (pt1 == pt2) …  INVALID Must be done as: if (pt1.x == pt2.x && pt1.y == pt2.y) …

structs and Functions, K&R 6.2 /* ptinrect: if point p in rect r, return 1 else return 0 note: slightly different from K&R example */ int ptinrect (struct point p, struct rect r) { return p.x >= r.pt1.x && p.x <= r.pt2.x && p.y >= r.pt1.y && p.y <= r.pt2.y; }

Arrays of structs, K&R 6.3 Multiple related arrays char * keyword[NKEYS]; int keycount[NKEYS]; Can be implemented as an array of structs struct key { char *word; int count; } keytab [NKEYS];

Arrays of structs Alternative array of structs implementation struct key { char *word; int count; }; struct key keytab[NKEYS];

Arrays of structs Initialization for an array of structs struct key { char *word; int count; } keytab[ ] = { “auto”, 0, … “while”, 0 }; /* NKEYS is dynamically derived */

Size of structs sizeof is a compile-time unary operator Can be used to get integer (actually size_t): sizeof object sizeof (type name) Applied to structs #define NKEYS (sizeof keytab / sizeof (struct key)) #define NKEYS (sizeof keytab / sizeof keytab[0])

Pointers to structs, K&R 6.4 Declare and initialize a pointer to struct struct point p; struct point *pp = &p; Refer to members of struct p via pointer pp (*pp).x and (*pp).y  See precedence More commonly done as: pp->x and pp->y  See precedence

Pointers to structs struct string { int len; char *cp; } *p; Expression Same as Value / Effect ++p->len ++(p->len) increments len *p->cp *(p->cp) value is a char *p->cp++ *((p->cp)++) value is a char increments cp

Pointers to structs /* ptinrect: (pointer version) if point p in rect r, return 1 else return 0 */ int ptinrect (struct point *pp, struct rect *rp) { return pp->x >= rp->pt1.x && pp->x <= rp->pt2.x && pp->y >= rp->pt1.y && pp->y <= rp->pt2.y; }