DOC PREVIEW
UW CSE 142 - Pointer Parameters

This preview shows page 1-2-16-17-18-34-35 out of 35 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

University of Washington Computer Programming IOverviewPowerPoint PresentationSlide 4Slide 5Slide 6Call By Value is Not EnoughSlide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35M-1University of WashingtonComputer Programming ILecture 13Pointer Parameters© 2000 UW CSEM-2OverviewConcepts this lectureFunction parametersCall by value (review)Pointer parameters - call by referencePointer types& and * operatorsM-3Reading6.1 Output (pointer) Parameters6.2 Multiple calls to functions with output parameters6.3 Scope of Names6.4 Passing Output Parameters to other functions 6.6, 6.7 Debugging and common programming errorsM-43 8 ?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-5Function Call ReviewRemember how function calls are executed:Allocate space for parameters and local variablesInitialize parameters by copying argument valuesBegin execution of the function bodyTrace carefully to get the right answerM-64 7X 3 X 8Trace/* 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 b4 7Output: 4 7move_one x yM-7Call By Value is Not EnoughOnce 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-8New Type: PointerA 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*)x32xpM-9xDeclaring and Using a Pointerint 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 */xp0X 1M-10int main ( void ) {int a, b ;a = 4 ; b = 7 ;move_one( &a , &b ) ;printf(“%d %d”, a, b);return 0;}Pointer Solution to move_oneThe & operator in front of a variable name creates a pointer to that variablevoid move _one ( int * x_ptr, int * y_ptr ) { *x_ptr = *x_ptr - 1;*y_ptr = *y_ptr + 1;}M-11Tracemain a b4 7move_one x_ptr y_ptrOutput: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 3 X 8M-12Tracemain a b4 7Output: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 3 X 83 8M-13Aliases*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-14Pointer TypesThree 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-15Pointer OperatorsTwo new (unary) operators:& “address of”& can be applied to any variable (or param)* “location pointed to by”* can be applied only to a pointerKeep track of the types: if x has type double, &x has type “pointer to double” or “double *”M-16VocabularyDereferencing or indirection:following a pointer to a memory locationThe 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-17Why 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 courseIn advanced programming, pointers are used to create dynamic data structures.M-18Now 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 RevisitedM-19Problem: 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 LineProgramming approach: We’d like to package this in a function(x1, y1)(x2, y2)(x1+x2) (y1+y2)2 2,( )M-20The (midx,midy) parameters are being altered, so they need to be pointersFunction SpecificationFunction 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(x1, y1)(x2, y2)(x1+x2) (y1+y2)2 2,( )M-21void 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(x1, y1)(x2, y2)(x1+x2) (y1+y2)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-22Traceset_midpoint x1 y1 x2 y2 midx_p midy_pmain x_end y_end mx my250.0 100.00.0 0.0 250.0 100.0125.050.0M-23Example: Gameboard CoordinatesBoard Coordinatesrow, column (used by players)Screen Coordinatesx, y (used by graphics package) (x,y)Problem: convert (x,y) to (row,col)M-24Coordinate Conversion: Analysisrowcol(LL_X, LL_Y) (x,y)x – LL_Xy – LL_YSQUARE_SIZE0 1 2 3 443210M-25void 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 10screen_to_board (x, y, &row, &col); *row_p = (screeny - LL_Y) / SQUARE_SIZE; *col_p = (screenx - LL_X) / SQUARE_SIZE;}M-26Problem: ReorderSuppose 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 pointersThis example is a small version of a very important problem in


View Full Document

UW CSE 142 - Pointer Parameters

Download Pointer Parameters
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Pointer Parameters 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 Pointer Parameters 2 2 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?