Homework Finishing K&R Chapter 5 today Starting K&R Chapter 6 next

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
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 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
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.
Introduction to Programming Lecture 39. Copy Constructor.
Structures A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
C Pointers Systems Programming Concepts. PointersPointers  Pointers and Addresses  Pointers  Using Pointers in Call by Reference  Swap – A Pointer.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Introduction to C Programming CE
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
1 Homework HW6 due class 22 K&R 6.6 K&R 5.7 – 5.9 (skipped earlier) Finishing up K&R Chapter 6.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
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.
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
Homework Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
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.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
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.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
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)
Ahmet Sacan - Ceng C-Programming: A Crash-Course in Pointers, Dynamic Memory, Structs, & Files Ceng302: Intro to DBMS Ahmet Sacan.
Windows Programming Lecture 03. Pointers and Arrays.
Generic Programming in C
C Structures and Memory Allocation
Chapter 5, Pointers and Arrays
Structures, Unions, Enumerations
Chapter 10-1: Structure.
Chapter 5 Classes.
Programming Languages and Paradigms
14th September IIT Kanpur
Buy book Online -
Homework / Exam Continuing K&R Chapter 6 Exam 2 after next class
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Alternate Version of STARTING OUT WITH C++ 4th Edition
Systems Programming Concepts
Return by Reference CSCE 121 J. Michael Moore.
Pointers Call-by-Reference CSCI 230
Miscellaneous functions
CS111 Computer Programming
Dynamic Memory A whole heap of fun….
CSCE 206 Lab Structured Programming in C
Homework Starting K&R Chapter 5 Good tutorial on pointers
Homework Continuing K&R Chapter 6.
Structures CSE 2031 Fall February 2019.
7. Pointers, Dynamic Memory
C Programming Lecture-8 Pointers and Memory Management
Homework Continue with K&R Chapter 5 Skipping sections for now
Structures CSE 2031 Fall April 2019.
Structures CSE 2031 Fall May 2019.
The Stack.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
Structures EECS July 2019.
C Structures and Memory Allocation
User-defined data types
pointer-to-pointer (double pointer)
EECE.2160 ECE Application Programming
Structures, Unions, and Enumerations
Presentation transcript:

Homework Finishing K&R Chapter 5 today Starting K&R Chapter 6 next Skipping sections 5.7-5.9 for now Not covering section 5.12 Starting K&R Chapter 6 next

Pointers to Functions, K&R 5.11 Function prototype with pointer to function void qsort ( … , int (*comp) (void *, void *)); Function call passing a pointer to function qsort( … , (int (*) (void *, void *)) strcmp); This is a cast to function pointer of strcmp Within qsort(), function is called via a pointer if ((*comp) (v[i], v[left]) < 0) … Address of a function Qsort will use comp function. Different sorting functions

Pointers to Functions Initialize a pointer to a function /* function pointer *fooptr = cast of foo to func ptr */ int (*fooptr) (int *, int*) = (int (*) (int *, int *)) foo; Call the function foo via the pointer to it (*fooptr) (to, from); This allows you to pass a function name dynamically to another function. We don’t do much with this concept in this course. END OF POINTERS

structs, K&R 6 A struct is a collection of variables, possibly of different types, grouped under a single name for common reference as a unit. struct point { /* with optional tag */ int x; /* member x */ int y; /* member y */ } pt, q; /* variable names */ or struct { /* w/o optional tag */ int x, y; /* two members */ } pt, q; /* variable names */ Size of them

structs The defined struct point is like a new “type” With the tag, point, can declare other variables: struct point pt1, maxpt = {320, 200}; Reference to struct members: pt1.x = 320; pt1.y = 200; /* alternate init */ printf("(%d, %d)\n", pt1.x, pt1.y); /* prints out as (320, 200) */ Struct.element

structs Defining a struct inside a struct (nesting). struct rect { struct point pt1; /* lower left */ struct point pt2; /* upper right */ }; struct rect box;/* declare box as a rect */ You can First define it then use it

structs pt1 is lower left hand corner of box pt2 is upper right hand corner of box: box.pt1.x < box.pt2.x box.pt1.y < box.pt2.y y box.pt2.y To get area box.pt1.y x box.pt1.x box.pt2.x

structs /* Find area of a rectangle */ int area = rectarea (box); … int rectarea (struct rect x) { return (x.pt2.x - x.pt1.x) * (x.pt2.y - x.pt1.y); }

structs Memory allocation for structs Two point structs pt1 and pt2 One rect struct box containing two point structs pt1 pt2 pt1.x pt1.y pt2.x pt2.y box pt1 pt2 pt1.x pt1.y pt2.x pt2.y