Pointers and Arrays. More Pointer Examples. Pointers CS 217

Similar documents
Pointers. Pointers. Pointers (cont) CS 217

cisc1110 fall 2010 lecture VI.2 call by value function parameters another call by value example:

Agenda & Reading. Class Exercise. COMPSCI 105 SS 2012 Principles of Computer Science. Arrays

Pointers. Pointer References

box Boxes and Arrows 3 true 7.59 'X' An object is drawn as a box that contains its data members, for example:

Programming. Example - Complex Numbers. add_complex step by step. add_complex step by step. add_complex step by step י"ט/טבת/תשע"א

Sample Midterm Solutions COMS W4115 Programming Languages and Translators Monday, October 12, 2009

Reference types and their characteristics Class Definition Constructors and Object Creation Special objects: Strings and Arrays

Discussion 1 Recap. COP4600 Discussion 2 OS concepts, System call, and Assignment 1. Questions. Questions. Outline. Outline 10/24/2010

Scope, Functions, and Storage Management

CS201 Discussion 10 DRAWTREE + TRIES

Mid-term exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Oct 25, Student's name: Student ID:

Lexical Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Functor (1A) Young Won Lim 10/5/17

ASTs, Regex, Parsing, and Pretty Printing

CPSC 213. Polymorphism. Introduction to Computer Systems. Readings for Next Two Lectures. Back to Procedure Calls

CSCE 531, Spring 2017, Midterm Exam Answer Key

Functor (1A) Young Won Lim 8/2/17

Introduction To Files In Pascal

Symbol Table management

Today s Lecture. Basics of Logic Design: Boolean Algebra, Logic Gates. Recursive Example. Review: The C / C++ code. Recursive Example (Continued)

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

Procedures vs. Functions. Procedure Call. Syntax. Activation Record for Nested Blocks. Environment. CSE 3302 Programming Languages

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex

Lecture Overview. Knowledge-based systems in Bioinformatics, 1MB602. Procedural abstraction. The sum procedure. Integration as a procedure

Stack. A list whose end points are pointed by top and bottom

Virtual Machine (Part I)

Matrices and Systems of Equations

10/9/2012. Operator is an operation performed over data at runtime. Arithmetic, Logical, Comparison, Assignment, Etc. Operators have precedence

Definition of Regular Expression

The Basic Properties of the Integral

Fig.25: the Role of LEX

CMPSC 470: Compiler Construction

Memory Management Functions

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lecture Writing Classes

Lists in Lisp and Scheme

2 Computing all Intersections of a Set of Segments Line Segment Intersection

Outline. Tiling, formally. Expression tile as rule. Statement tiles as rules. Function calls. CS 412 Introduction to Compilers

Reducing a DFA to a Minimal DFA

In the last lecture, we discussed how valid tokens may be specified by regular expressions.

Lecture 10 Evolutionary Computation: Evolution strategies and genetic programming

CIS 1068 Program Design and Abstraction Spring2015 Midterm Exam 1. Name SOLUTION

Systems I. Logic Design I. Topics Digital logic Logic gates Simple combinational logic circuits

COMPUTER SCIENCE 123. Foundations of Computer Science. 6. Tuples

1.1. Interval Notation and Set Notation Essential Question When is it convenient to use set-builder notation to represent a set of numbers?

Unit #9 : Definite Integral Properties, Fundamental Theorem of Calculus

10.5 Graphing Quadratic Functions

Data sharing in OpenMP

Summer Review Packet For Algebra 2 CP/Honors

Computer Arithmetic Logical, Integer Addition & Subtraction Chapter

Stack Manipulation. Other Issues. How about larger constants? Frame Pointer. PowerPC. Alternative Architectures

PARALLEL AND DISTRIBUTED COMPUTING

Topics in Analytic Geometry

PYTHON PROGRAMMING. The History of Python. Features of Python. This Course

From Dependencies to Evaluation Strategies

Presentation Martin Randers

What do all those bits mean now? Number Systems and Arithmetic. Introduction to Binary Numbers. Questions About Numbers

CS 241. Fall 2017 Midterm Review Solutions. October 24, Bits and Bytes 1. 3 MIPS Assembler 6. 4 Regular Languages 7.

called the vertex. The line through the focus perpendicular to the directrix is called the axis of the parabola.

CSE 401 Midterm Exam 11/5/10 Sample Solution

Exam #1 for Computer Simulation Spring 2005

Product of polynomials. Introduction to Programming (in C++) Numerical algorithms. Product of polynomials. Product of polynomials

5 Regular 4-Sided Composition

L2-Python-Data-Structures

Lecture 7: Integration Techniques

Example: Source Code. Lexical Analysis. The Lexical Structure. Tokens. What do we really care here? A Sample Toy Program:

Engineer To Engineer Note

Union-Find Problem. Using Arrays And Chains. A Set As A Tree. Result Of A Find Operation

Reducing Costs with Duck Typing. Structural

Arrays as functions. Types. Multidimensional Arrays (row major, column major form) Java arrays

EXPONENTIAL & POWER GRAPHS

MA1008. Calculus and Linear Algebra for Engineers. Course Notes for Section B. Stephen Wills. Department of Mathematics. University College Cork

Solution of Linear Algebraic Equations using the Gauss-Jordan Method

Dynamic Programming. Andreas Klappenecker. [partially based on slides by Prof. Welch] Monday, September 24, 2012

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Algebra II Notes Unit Ten: Conic Sections

LAB L Hardware Building Blocks

How to Design REST API? Written Date : March 23, 2015

Context-Free Grammars

Essential Question What are some of the characteristics of the graph of a rational function?

COMP 423 lecture 11 Jan. 28, 2008

- 2 U NIX FILES 1. Explin different file types vilble in UNIX or P OSIX s ystem. ( 08 mrks) ( My-08/Dec-08/My-10/My- 12) 2. Wht is n API? How is it di

Information Retrieval and Organisation

Hyperbolas. Definition of Hyperbola

Suffix trees, suffix arrays, BWT

Basics of Logic Design Arithmetic Logic Unit (ALU)

George Boole. IT 3123 Hardware and Software Concepts. Switching Algebra. Boolean Functions. Boolean Functions. Truth Tables

COMMON HALF YEARLY EXAMINATION DECEMBER 2018

Introduction to Scientific Computing and Problem Solving

Graphing Conic Sections

The Fundamental Theorem of Calculus

Questions About Numbers. Number Systems and Arithmetic. Introduction to Binary Numbers. Negative Numbers?

EECS 281: Homework #4 Due: Thursday, October 7, 2004

CS 241 Week 4 Tutorial Solutions

Grade 7/8 Math Circles Geometric Arithmetic October 31, 2012

vcloud Director Service Provider Admin Portal Guide vcloud Director 9.1

Compression Outline :Algorithms in the Real World. Lempel-Ziv Algorithms. LZ77: Sliding Window Lempel-Ziv

Section 5.3 : Finding Area Between Curves

TO REGULAR EXPRESSIONS

Transcription:

Pointers nd Arrs CS 21 1 2 Pointers More Pointer Emples Wht is pointer A vrile whose vlue is the ddress of nother vrile p is pointer to vrile v Opertions &: ddress of (reference) *: indirection (dereference) p: v: 11 1000 2 11 References (e.g., *p) re vriles int,, *p, *p; Declrtion mimics use int *p; p is the ddress of n int (dereference p is n integer) int v; p = &v; p stores the ddress of v 0 4

More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ p p p p 5 6 More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p p p p 8

More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p = p; /* p lso points to */ 0 p 0 p p 9 p 10 More Pointer Emples More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p = p; /* p lso points to */ p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p = p; /* p lso points to */ *p += 1; /* increments to 1 */ 0 0 p p 11 p p 12

More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p = p; /* p lso points to */ *p += 1; /* increments to 1 */ More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p = p; /* p lso points to */ *p += 1; /* increments to 1 */ = (*p)++; /* sets to 1, to 2 */ 1 1 p p 1 p p 14 More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p = p; /* p lso points to */ *p += 1; /* increments to 1 */ = (*p)++; /* sets to 1, to 2 */ More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p = p; /* p lso points to */ *p += 1; /* increments to 1 */ = (*p)++; /* sets to 1, to 2 */ 1 1 2 1 p p 15 p p 16

Opertor Precedents Unr opertors ssocite right to left = *&; /* sme s = *(&) */ Unr opertors ind more tightl thn inr ones = *p + 1; /* sme s = (*p) + 1; */ More emples = *p++; /* sme s = *p; p++; */ = *(p++); /* sme s ove */ = *++p; /* sme s p++; = *p; */ = ++*p; /* sme s = (*p) + 1; */ When in dout, lierll use prentheses Argument Pssing C functions pss rguments vlue void swp(int, int ) t = ; = ; = t; int =, = ; swp(, ); printf( %d %d\n,,); 1 18 Argument Pssing C functions pss rguments vlue Argument Pssing C functions pss rguments vlue void swp(int, int ) t = ; = ; = t; int =, = ; swp(, ); printf( %d %d\n,,); void swp(int, int ) t = ; = ; = t; int =, = ; swp(, ); printf( %d %d\n,,); 19 20

Argument Pssing C functions pss rguments vlue To pss rguments reference, use pointers void swp(int, int ) t = ; = ; = t; int =, = ; swp(, ); printf( %d %d\n,,); void swp(int *, int t = *; * = *; * = t; int =, = ; swp(&, &); printf( %d %d\n,,); *) Argument Pssing C functions pss rguments vlue To pss rguments reference, use pointers void swp(int, int ) t = ; = ; = t; int =, = ; swp(, ); printf( %d %d\n,,); void swp(int *, int t = *; * = *; * = t; int =, = ; swp(&, &); printf( %d %d\n,,); *) 21 22 Argument Pssing C functions pss rguments vlue To pss rguments reference, use pointers void swp(int, int ) t = ; = ; = t; int =, = ; swp(, ); printf( %d %d\n,,); void swp(int *, int t = *; * = *; * = t; int =, = ; swp(&, &); printf( %d %d\n,,); *) Formtted Input: scnf Emple doule v; scnf( %lf, &v ); int d, month, er; scnf( %d/%d/%d, &month, &d, &er); 2 24

Pointers nd Arrs Pointers cn wlk long rrs int [10], *p, ; p = &[0]; /* p gets the ddress of [0] */ = *p; /* gets [0] */ = *(p+1); /* gets [1] */ p = p + 1; /* p points to [1] */ p++; /* p points to [2] */ p 25 26 Pointers nd Arrs Pointers cn wlk long rrs int [10], *p, ; p = &[0]; /* p gets the ddress of [0] */ = *p; /* gets [0] */ = *(p+1); /* gets [1] */ p = p + 1; /* p points to [1] */ p++; /* p points to [2] */ Pointers nd Arrs Pointers cn wlk long rrs int [10], *p, ; p = &[0]; /* p gets the ddress of [0] */ = *p; /* gets [0] */ = *(p+1); /* gets [1] */ p = p + 1; /* p points to [1] */ p++; /* p points to [2] */ p p 2 28

Pointers nd Arrs, cont d Arr nmes re constnt pointers int [10], *p, i; p = ; /* p points to [0] */ = p; /* Illegl; cn t chnge constnt */ ++; /* Illegl; cn t chnge constnt */ p++; /* Legl; p is vrile */ Suscripting is defined in terms of pointers [i], *(+i), i[] /* Legl nd the sme */ &[i], +i /* Legl nd the sme */ p = &[0] /* &*(+0) &* */ Pointers cn wlk rrs efficientl p = ; for (i = 0; i < 10; i++) printf( %d\n, *p++ ); 29 Pointer Arithmetic n o w i s t h e t i m e \0 1 0 0 0 2 0 0 0 0 0 0 4 0 0 0 long *p; Pointer rithmetic tkes into ccount the stride (size of) the vlue pointed to long *p; p += i; /* increments p i elements */ p -= i; /* decrements p i elements */ p++; /* increments p 1 element */ p--; /* decrements p 1 element */ If p nd q re pointers to sme tpe T p q /* numer of elements (longs, or chrs) etween p nd q */ Does it mke sense to dd two pointers? chr *p; 0 Pointer Arithmetic, cont d Pointer & Arr Prmeters Comprison opertions for pointers <, >, <=, >=, ==,!= if (p < q)... ; p nd q must point to the sme rr no runtime checks to ensure this An emple int strlen(chr *s) chr *p; for (p = s; *p; p++) ; return p s; n o w i s t h e t i m e \0 Formls re not constnt; the re vriles Pssing n rr psses pointer to 1st element Arrs (nd onl rrs) re pssed reference Declrtion: void f(int [])... is equivlent to void f(int *)... = +1; Cll: int [10];... f(); 1 2 s p p

Pointers & Strings An Emple: String Cop A C string is n rr of chr with NULL t the end n o w i s t h e t i m e \0 String constnts denote constnt pointers to ctul chrs chr *msg = now is the time ; chr msg[] = now is the time ; chr *msg = msg; /* msg points to 1st chrcter of now is the time */ Strings cn e used whenever rrs of chrs re used sttic chr digits[] = 01245689 ; putchr( 01245689 [i]); putchr(digits[i]); Pointers nd rrs re essentill the sme thing => Pointers to chrs nd rrs of chrs re the sme Arr version void scop(chr s[], chr t[]) int i = 0; while ((s[i] = t[i])!= \0 ) i++; Pointer version void scop(chr *s, chr *t) while (*s = *t) s++; t++; Idiomtic version void scop(chr s[], chr t[]) while (*s++ = *t++) ; 4 Arrs of Pointers Arrs of Pointers, cont d Used to uild tulr structures Declre rr of pointers to strings chr *line[100]; chr *(line[100]); /* sme s ove */ chr (*line)[100]; /* never used */ Initiliztion emple chr *month(int n) sttic chr *nme[] = Jnur, Ferur, Mrch, April, M, June, Jul, August, Septemer, Octoer, Novemer, Decemer ; Reference emples line[i] /* refers to the i-th string */ *line[i] /* refers to the 0-th chr of the i-th string */ ssert(n >= 1 && n <= 12); return nme[n-1]; Another emple int, ; int *[] = &, &, &, &, NULL; 5 6

Arrs of Pointers, cont d An rr of pointers is 2-D rr int [10][10]; int *[10]; Arr : 2-dimensionl 1010 rr Storge for 100 elements llocted t compile time Ech row of hs 10 elements, cnnot chnge t runtime [6] is constnt 2-d rrs re stored in row-mjor order row 2 row 1 row 0 Arr : An rr of 10 pointers; ech element could point to n rr Storge for 10 pointers llocted t compile time Vlues of these pointers must e initilized t runtime Ech row of cn hve different length (rgged rr) [6] is vrile; [i] cn chnge t runtime More Emples Equivlence emple void f(int *[10]); /* known numer of rows */ void f(int **); Another equivlnce emple void g(int [][10]); /* known numer of columns */ void g(int (*)[10]); Legl in oth f nd g: ** = 1; 8 Commnd-Line Arguments Pointers to Functions B convention, min() is clled with 2 rguments int min(int rgc, chr *rgv[]) rgc is the numer of rguments, including the progrm nme rgv is n rr of pointers to the rguments Emple: % echo hello rgc = 2 rgv[0] = echo rgv[1] = hello rgv[2] = NULL Implementtion of echo min(int rgc, chr *rgv[]) int i; for (i = 1; i < rgc; i++) printf( %s%c,rgv[i], (i < rgc-1)? : \n ); eit(0); 9 #include <stdio.h> int dd(int, int ) return + ; int mul(int, int ) return * ; min() int [5] = 1, 2,, 4, 5; int sum = doarr(, 5, 0, dd); int prod = doarr(, 5, 1, mul); printf("sum = %d, product = %d\n", sum, prod); int doarr(int [], int n, int vl, int (*op)(int, int)) int i; for (i = 0; i < n; i++) vl = (*op)(vl, [i]); /* vl = op(vl, [i]); */ return vl; 40

Pointers to Functions, cont d Declrtion snt cn e confusing: int (*op)(int, int) declres op to e pointer to function tht tkes two int rguments nd returns n int int *op(int, int) declres op to e function tht tkes two int rguments nd returns pointer to n int Invoction snt cn lso confuse: (*op)(, ) clls the function pointed to op with the rguments nd, equivlent to op(, ) *op(, ) clls the function op with rguments nd, then dereferences the vlue returned Pointers to Functions, cont d A function nme itself is constnt pointer to function (like n rr nme) int dd(int, int ) int mul(int, int ) int sum = doarr(, 5, 0, dd); int prod = doarr(, 5, 1, mul); Function cll hs higher precedence thn dereferencing 41 42 Pointers to Functions, cont d Arrs of pointers to functions etern int mul(int, int); etern int dd(int, int);... int (*opertors[])(int, int) = mul, dd,... ; To invoke (*opertors[i])(, ); Summr Pointers tpe * (int *p) declres pointer vrile * nd & re the ke opertions Opertion rules Unr opertions ind more tightl thn inr ones Pointer rithmetic opertions consider size of the elements Pointers nd rrs hve tight reltionship An rr is constnt pointer pointing to the 1 st element A pointer cn wlk through elements of n rr An rr of pointers is 2-D rr (1-D fied nd nother vrile) Mster how to get commnd-line rguments from min() 4 Pointers to functions Cn e used to prmeterize functions 44