Unformatted text preview:

CSE 142 Computer Programming I Overview Concepts this lecture Function parameters Call by value review Pointer parameters call by reference Pointer types and operators Pointer Parameters 2000 UW CSE M 1 Reading M 2 What Does This Print change x and y void move one int x int y x x 1 y y 1 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 3 int main void int a b a 4 b 7 move one a b printf d d a b return 0 Output 3 8 4 7 M 4 Trace Function Call Review move one 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 4 3 X x 78 X y main Trace carefully to get the right answer 4 7 a b 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 M 5 Output 4 7 M 6 M 1 Call By Value is Not Enough New Type Pointer Once the function parameters are initialized with copies of the arguments there is no further connection A pointer contains a reference to another variable that is a pointer contains the memory address of a variable 32 x 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 7 Declaring and Using a Pointer xp has type pointer to int often written xp has type int xp M 8 Pointer Solution to move one void move one int x ptr int y ptr int x declares an int variable int xp declares a pointer to int x ptr x ptr 1 y ptr y ptr 1 If the address of x is stored in xp then xp 0 Assign integer 0 to x xp xp 1 Add 1 to x X1 0 xp x M 9 Trace The operator in front of a variable name creates a pointer to that variable M 10 Trace move one x ptr int main void int a b a 4 b 7 move one a b printf d d a b return 0 y ptr main 4 3 X X 7 8 a b 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 Output M 11 void move one int x ptr int y ptr x ptr x ptr 1 y ptr y ptr 1 main 4 3 X 7 8 X a b a 4 b 7 move one a b Output 3 8 M 12 M 2 Aliases Pointer Types x ptr and y ptr act like aliases for the variables a and b in the function call Three new types int pointer to int double pointer to double char pointer to char When you change x ptr and y ptr you are changing the values of the caller s variables 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 To create these aliases you need to use a b in the call M 13 M 14 Vocabulary 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 15 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 16 Why Use Pointers scanf Revisited For parameters Now we can make sense out of the punctuation in scanf 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 int x y z scanf d d d x y x y scanf d d x y In advanced programming pointers are used to create dynamic data structures M 17 NO YES Why M 18 M 3 Example Midpoint Of A Line Function Specification Problem Find the midpoint of a line segment Function specification given endpoints x1 y1 and x2 y2 of a line segment store the coordinates of the midpoint in midx midy x2 y2 Algorithm find the average of the coordinates of the endpoints x y 1 xmid x1 x2 2 0 ymid y1 y2 2 0 x2 y2 Parameters x1 y1 x2 y2 midx and midy 1 x 2 x y2 y 1 2 1 The midx midy parameters are being altered so they need to be pointers 2 Programming approach We d like to package this in a function x1 y1 x 2 x y2 y 1 2 1 M 19 M 20 Midpoint Function Code Trace 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 2 x2 y2 double x end y end mx my x end 250 0 y end 100 0 x1 y1 set midpoint 0 0 0 0 x1 x2 y1 y2 x end y end 2 2 M 21 mx my Example Gameboard Coordinates set midpoint 0 0 0 0 250 0 x1 y1 x2 100 0 y2 midx p midy p main 250 0 x end 125 0 100 0 y end 50 0 mx my M 22 Coordinate Conversion Analysis 4 3 Board Coordinates row row column used by players Screen Coordinates 1 x y x y used by graphics package Problem convert x y to row col 2 x y y LL Y 0 SQUARE SIZE x LL X LL X LL Y M 23 0 1 2 col 3 4 M 24 M 4 Coordinate Conversion Code Problem Reorder define LL X 40 define LL Y 20 define SQUARE SIZE 10 Suppose we want a function to arrange its two parameters in reverse numeric order void screen to board int screenx int screeny coords on screen int row p int col p position on board row p screeny LL Y SQUARE SIZE col p screenx LL X SQUARE SIZE screen to board x y row col M 25 Example 1 5 need to be reordered as 5 1 12 3 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 …


View Full Document

UW CSE 142 - Study Notes

Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?