M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

Pointers Pointer is a variable that contains the address of a variable Here P is sahd to point to the variable C C 7 34……
Computer Programming w/ Eng. Applications
Programming and Data Structure
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointers in C Rohit Khokher
Kernighan/Ritchie: Kelley/Pohl:
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
1 CSE1301 Computer Programming Lecture 16 Pointers.
Pointers Ethan Cerami Fundamentals of Computer New York University.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
1 CSE1301 Computer Programming Lecture 16 Pointers.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Computer Science 210 Computer Organization Pointers.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
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:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.
Pointers *, &, array similarities, functions, sizeof.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
H1-1 University of Washington Computer Programming I Lecture 9: Iteration © 2000 UW CSE.
Functions Programming Applications. Functions every C program must have a function called main program execution always begins with function main any.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4;
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
Computer Programming Lecture 12 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Problem Solving and Program Design in C Chap. 6 Pointers and Modular Programming Chow-Sing Lin.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Parallel Arrays? ANSI-C.
User-Written Functions
Computer Science 210 Computer Organization
CSE 220 – C Programming Pointers.
EPSII 59:006 Spring 2004.
ICS103 Programming in C Lecture 3: Introduction to C (2)
INC 161 , CPE 100 Computer Programming
CSI-121 Structured Programming Language Lecture 16 Pointers
Computer Science 210 Computer Organization
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Programming in C Pointer Basics.
Programming in C Pointer Basics.
Simulating Reference Parameters in C
Exercise Arrays.
Presentation transcript:

M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE

M-2 Overview Concepts this lecture Function parameters Call by value (review) Pointer parameters - call by reference Pointer types & and * operators

M-3 Reading 6.1 Output (pointer) Parameters 6.2 Multiple calls to functions with output parameters 6.3 Scope of Names 6.4 Passing Output Parameters to other functions 6.6, 6.7 Debugging and common programming errors

M ? 4 7 ? What Does This Print? int main ( void ) { int a, b ; a = 4 ; b = 7 ; move_one(a, b) ; printf(“%d %d”, a,b); return 0; } Output: /* change x and y */ void move_one ( int x, int y ) { x = x - 1; y = y + 1; }

M-5 Function Call Review Remember how function calls are executed: Allocate space for parameters and local variables Initialize parameters by copying argument values Begin execution of the function body Trace carefully to get the right answer

M-6 47X 3X 8 Trace /* change x and y */ void move_one ( int x, int y ) { x = x - 1; y = y + 1; } int main ( void ) { int a, b ; a = 4 ; b = 7 ; move_one(a, b) ; printf(“%d %d”, a,b); return 0; } main a b 47 Output:4 7 move_one x y

M-7 Call By Value is Not Enough Once the function parameters are initialized with copies of the arguments, there is no further connection. If the function changes its parameters, it affects the local copy only. To actually change the arguments in the caller, the function needs access to the locations of the arguments, not just their values.

M-8 New Type: Pointer A pointer contains a reference to another variable; that is, a pointer contains the memory address of a variable. xp has type pointer to int (often written: xp has type int*) x 32 xp

M-9 x Declaring and Using a Pointer int x; /* declares an int variable */ int * xp; /* declares a pointer to int */ If the address of x is stored in xp, then: *xp = 0; /* Assign integer 0 to x */ *xp = *xp + 1; /* Add 1 to x */ xp 0 X 1

M-10 int main ( void ) { int a, b ; a = 4 ; b = 7 ; move_one( &a, &b ) ; printf(“%d %d”, a, b); return 0; } Pointer Solution to move_one The & operator in front of a variable name creates a pointer to that variable void move _one ( int * x_ptr, int * y_ptr ) { *x_ptr = *x_ptr - 1; *y_ptr = *y_ptr + 1; }

M-11 Trace main a b 47 move_one x_ptr y_ptr Output: void move_one ( int * x_ptr, int * y_ptr ) { *x_ptr = *x_ptr - 1; *y_ptr = *y_ptr + 1; } a = 4 ; b = 7 ; move_one( &a, &b ) ; X 3X 8

M-12 Trace main a b 47 Output: void move_one ( int * x_ptr, int * y_ptr ) { *x_ptr = *x_ptr - 1; *y_ptr = *y_ptr + 1; } a = 4 ; b = 7 ; move_one( &a, &b ) ; X 3X 8 3 8

M-13 Aliases *x_ptr and *y_ptr act like aliases for the variables a and b in the function call. When you change * x_ptr and * x_ptr you are changing the values of the caller’s variables. To create these aliases you need to use &a, &b in the call.

M-14 Pointer Types Three new types: int * “pointer to int” double *“pointer to double” char *“pointer to char” These are all different - a pointer to a char can’t be used if the function parameter is supposed to be a pointer to an int, for example.

M-15 Pointer Operators Two new (unary) operators: &“address of” & can be applied to any variable (or param) *“location pointed to by” * can be applied only to a pointer Keep track of the types: if x has type double, &x has type “pointer to double” or “double *”

M-16 Vocabulary Dereferencing or indirection: following a pointer to a memory location The book calls pointer parameters “output parameters”: can be used to provide a value ("input") as usual, and/or store a changed value ("output") Don’t confuse with printed output (printf)

M-17 Why Use Pointers? For parameters: in functions that need to change their actual parameters( such as move_one) in functions that need multiple “return” values (such as scanf) These are the only uses in this course In advanced programming, pointers are used to create dynamic data structures.

M-18 Now we can make sense out of the punctuation in scanf int x,y,z; scanf(“%d %d %d”, x, y, x+y); NO! scanf(“%d %d”, &x, &y); YES! Why? scanf Revisited

M-19 Problem: Find the midpoint of a line segment. Algorithm: find the average of the coordinates of the endpoints: xmid = (x1+x2)/2.0; ymid = (y1+y2)/2.0; Example: Midpoint Of A Line Programming approach: We’d like to package this in a function (x 1, y 1 ) (x 2, y 2 ) (x 1 +x 2 ) (y 1 +y 2 ) 2, ()

M-20 The (midx,midy) parameters are being altered, so they need to be pointers Function Specification Function specification: given endpoints (x1,y1) and (x2,y2) of a line segment, store the coordinates of the midpoint in (midx, midy) Parameters: x1, y1, x2, y2, midx, and midy (x 1, y 1 ) (x 2, y 2 ) (x 1 +x 2 ) (y 1 +y 2 ) 2, ()

M-21 void set_midpoint( double x1, double y1, double x2, double y2, double *  midx_p, double *  midy_p ) { *midx_p = (x1 + x2) / 2.0; *midy_p = (y1 + y2) / 2.0; } Midpoint Function: Code (x 1, y 1 ) (x 2, y 2 ) (x 1 +x 2 ) (y 1 +y 2 ) 2, () double x_end, y_end, mx, my; x_end = 250.0; y_end = 100.0; set_midpoint(0.0, 0.0, x_end, y_end, &mx, &my);

M-22 Trace set_midpoint x1y1x2y2midx_pmidy_p main x_endy_endmx my

M-23 Example: Gameboard Coordinates Board Coordinates row, column (used by players) Screen Coordinates x, y (used by graphics package) (x,y) Problem: convert (x,y) to (row,col)

M-24 Coordinate Conversion: Analysis row col (LL_X, LL_Y) (x,y) x – LL_X y – LL_Y SQUARE_SIZE

M-25 void screen_to_board ( int screenx, int screeny, /* coords on screen */ int *  row_p, int *  col_p)/* position on board */ { Coordinate Conversion: Code #define LL_X 40 #define LL_Y 20 #define SQUARE_SIZE 10 screen_to_board (x, y, &row, &col); *row_p = (screeny - LL_Y) / SQUARE_SIZE; *col_p = (screenx - LL_X) / SQUARE_SIZE; }

M-26 Problem: Reorder Suppose we want a function to arrange its two parameters in numeric order. Example: 12, 3 need to be reordered as 3, 12 -1, 5 is already in order (no change needed) Parameter analysis: since we might change the parameter values, they have to be pointers This example is a small version of a very important problem in computer science, called “sorting”

M-27 Code for Reorder /* ensure *p1 >= *p2, interchanging values if needed */ void reorder(int *p1, int *p2) { int tmp; if (*p1 < *p2) { tmp = *p1; *p1 = *p2; *p2 = tmp; }

M-28 swap as a Function /* interchange *p and *q */ void swap ( int *  p, int *  q) { int temp ; temp= *p ; *p= *q ; *q= temp ; } int a, b ; a = 4; b = 7;... swap (&a, &b) ;

M-29 Reorder Implemented using swap /* ensure *p1 >= *p2, interchanging values if needed */ void reorder(int *p1, int *p2) { if (*p1 < *p2) swap( ____, _____ ); } What goes in the blanks?

M-30 Pointer Parameters (Wrong!) Normally, if a pointer is expected, we create one using &: /* ensure *p1 >= *p2, interchanging values if needed */ void reorder(int *p1, int *p2) { if (*p1 < *p2) swap( &p1, &p2 ); } But that can’t be right - p1 and p2 are already pointers! What are the types of expressions &p1 and &p2?

M-31 Pointer Parameters (Right!) Right answer: if the types match (int *), we use the pointers directly /* ensure *p1 >= *p2, interchanging values if needed */ void reorder(int *p1, int *p2) { if (*p1 < *p2) swap( p1, p2 ); }

M-32 swap pq temp 17 Trace main xy reorder p1 p2 void swap(int *p, int *q){ … } void reorder(int*p1, int*p2) { if (*p1 < *p2) swap(p1,p2); } int x, y; x = 17; y = 42; reorder(&x,&y); 1742 X 42 X 17

M-33 Pointers and scanf Once More Problem: User is supposed to enter ‘y’ or ‘n’, and no other answer is acceptable. Read until user enters ‘y’ or ‘n’ and return input void Read_y_or_n(char *chp) {... } int main(void) { char ch; Read_y_or_n(&ch);...

M-34 Pointers and scanf Once More /* read until user enters ‘y’ or ‘n’ and return input */ void Read_y_or_n(char *chp) { printf(“Enter an ‘y’ or a ‘n’.\n”); scanf(“%c”, chp); while ( *chp != ‘y’ && *chp != ‘n’) { printf ("\nSorry, try again\n"); scanf(“%c”, chp); } int main(void) { char ch; Read_y_or_n(&ch);... No ‘&’ !

M-35 Wrapping Up Pointers are needed when the parameter value may be changed & creates a pointer * dereferences the value pointed to This completes the technical discussion of functions in C for this course Learning how to design and use functions will be a continuing concern in the course