DOC PREVIEW
Penn CIS 240 - Pointers and Arrays

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 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 10 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 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Based on slides © McGraw-HillAdditional material © 2004/2005 Lewis/MartinChapter 16Pointers andArrays2CSE 240Pointers and Arrays We've seen examples of both of thesein our LC-3 programs; now we'll see them in C Pointer• Address of a variable in memory• Allows us to indirectly access variablesIn other words, we can talk about its addressrather than its value Array• A list of values arranged sequentially in memory• Expression a[4] refers to the 5th element of the array a• Example: video memory in BreakOut (2D)3CSE 240x3107x2819x0110x0310x0100x1110x11B1x0019x3100x3101x3102x3103x3104x3105x3106x3107x3100R2addressvalueAddress vs. Value Sometimes we want to deal with the address of a memorylocation, rather than the value it contains Adding a column of numbers in LC-3:• R2 contains address of first location• Read value, add to sum, andincrement R2 until all numbershave been processed R2 is a pointer• It contains the address of data• (It’s also an array, but more on that later)4CSE 240Another Need for Addresses Consider the following function that's supposed toswap the values of its arguments.void swap_wrong(int first, int second){ int temp = first; first = second; second = temp;}What’s wrong with this code?25CSE 240Executing the Swap Function firstsecondb a 3443R6before calltemp firstsecondb a 34343R6after callThese valueschanged......but thesedid not.Swap needs addresses of variables outside its ownactivation recordswapmain6CSE 240Pointers in C C lets us talk about and manipulate pointersas variables and in expressions. Declaration int *p; /* p is a pointer to an int */ A pointer in C is always a pointer to a particular data type:int*, double*, char*, etc. Operators *p -- returns the value pointed to by p &z -- returns the address of variable z7CSE 240 int i; int *ptr; i = 4; ptr = &i; *ptr = *ptr + 1; printf(“%d\n”, i);Exampleread the contents of memoryat the address stored in ptrprint the value “5”, becaue “i” wasmodified indirectly via ptrptristore the value 4 intothe memory locationassociated with i4xEFF9xEFFAxEFFBxEFFCxEFFDxEFFEstore the address of “i”into the memory locationassociated with ptrxEFFCstore the result into memoryat the address stored in ptr 58CSE 240Example: LC-3 Code ; i is 1st local (offset 0), ptr is 2nd (offset 1) ; i = 4; AND R0, R0, #0 ; clear R0ADD R0, R0, #4 ; put 4 in R0STR R0, R6, #0 ; store in i; ptr = &i;ADD R0, R6, #0 ; R0 = R6 + 0 (addr of i)STR R0, R6, #1 ; store in ptr ; *ptr = *ptr + 1;LDR R0, R6, #1 ; R0 = ptrLDR R1, R0, #0 ; load contents (*ptr)ADD R1, R1, #1 ; add oneSTR R1, R0, #0 ; store to *ptr39CSE 240Pointers as Arguments Passing a pointer into a function allows the functionto read/change memory outside its activation record void swap(int *first, int *second){ int temp = *first; *first = *second; *second = temp;} How would you do this in Java?Arguments areinteger pointers.Caller passes addressesof variables that it wantsfunction to change All arguments in C are pass-by-value. Also true in Java, but Java has reference types10CSE 240Passing Pointers to a Function main() wants to swap the values of “a” and “b” passes the addresses to swap(): swap(&a, &b); Code for passing arguments: ADD R0, R6, #0 ; addr of bSTR R0, R6, #-1 ADD R0, R6, #1 ; addr of aSTR R0, R6, #-2tempfirstsecondbaxEFFAxEFF943R6xEFF9xEFFAxEFFBxEFFCxEFFDxEFFE11CSE 240Code Using Pointers Inside the swap() routine tempfirstsecondba3xEFFAxEFF943R6xEFF9xEFFAxEFFBxEFFCxEFFDxEFFE; int temp = *first;LDR R0, R6, #4 ; R0=xEFFALDR R1, R0, #0 ; R1=M[xEFFA]=3STR R1, R6, #0 ; temp=3; *first = *second;LDR R1, R6, #5 ; R1=xEFF9LDR R2, R1, #0 ; R1=M[xEFF9]=4LDR R0, R6, #4 ; R0=xEFFASTR R2, R0, #0 ; M[xEFFA]=4 ; *second = temp;LDR R2, R6, #0 ; R2=3LDR R1, R6, #5 ; R1=xEFF9STR R2, R1, #0 ; M[xEFF9]=3 4 312CSE 240Using Arguments for Results Pass address of variable where you want result stored• Useful for multiple results• Example:Return value via pointerReturn status code as function result This solves the mystery of the ‘&’ for calling scanf(): scanf("%d %d", &data1, &data2);read decimal integers into data1 and data2413CSE 240Null Pointer Sometimes we want a pointer that points to nothing. In other words, we declare a pointer, but we’re not readyto actually point to something yet. int *p;p = NULL; /* p is a null pointer */ NULL is a predefined macro that contains a value thata non-null pointer should never hold.• Often, NULL = 0, because Address 0 is not a legal addressfor most programs on most platforms• Dereferencing a NULL pointer: program crash!int *p = NULL; printf(“%d”, *p); // CRASH!14CSE 240Pointer Problems What does this do?int *x;*x = 10; Answer: writes “10” into a random location in memory• What would java do? What’s wrong with:int* func(){ int x = 10; return &x;} Answer: storage for “x” disappears on return, so thereturned pointer is dangling• What would java do?15CSE 240Declaring Pointers The * operator binds to the variable name, not the type All the same:• int* x, y;• int *x, y;• int *x; int y; Suggested solution: Declare only one variable per line• Avoids this problem• Easier to comment• Clearer• Don’t worry about “saving space”16CSE 240Arrays How do we allocate a group of memory locations?• Character string• Table of numbers How about this? Not too bad, but…• What if there are 100 numbers?• How do we write a loop to process each number? Fortunately, C gives us a better way -- the array. int num[4]; Declares a sequence of four integers, referenced by:num[0], num[1], num[2], num[3].int num0;int num1;int num2;int num3;517CSE 240Array Syntax Declaration type variable[num_elements]; Array Reference variable[index];all array elementsare of the same typenumber of elements must beknown at compile-timei-th element of array (starting with zero);no limit checking at compile-time or run-time18CSE 240Array as a Local Variable Array elements are allocatedas part of the activation record int grid[10]; First element (grid[0])is at lowest addressof allocated spacegrid[0]grid[1]grid[2]grid[3]grid[4]grid[5]grid[6]grid[7]grid[8]grid[9]19CSE 240LC-3 Code for Array References ; x = grid[3] + 1 ADD R0, R6, #1 ; R0 = &grid[0]LDR R1, R0, #3 ; R1 = grid[3]ADD R1, R1, #1 ; plus 1STR R1, R6, #0 ; x = R1 ; grid[6] = 5;AND R0, R0, #0ADD R0, R0, #5 ; R0 = 5ADD R1, R6, #1 ; R1 = &grid[0]STR R0, R1, #6 ; grid[6] =


View Full Document

Penn CIS 240 - Pointers and Arrays

Download Pointers and Arrays
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 Pointers and Arrays 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 Pointers and Arrays 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?