Lecture Introduction to computing systems (2/e): Chapter 17 - Yale N. Patt, Sanjay J. Patel

Chapter 17 - Pointers and arrays. This chapter presents the following content: Declaring pointer variables, address vs. value, another need for addresses, executing the swap function, pointers as arguments, passing pointers to a function, code using pointers,.and other contents. | Chapter 17 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-2 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 address rather than its value Array A list of values arranged sequentially in memory Example: a list of telephone numbers Expression a[4] refers to the 5th element of the array a 17- Address vs. Value Sometimes we want to deal with the address of a memory location, rather than the value it contains. Recall example from Chapter 6: adding a column of numbers. R0 contains address of first location. Read value, add to sum, and increment R0 until all numbers have been processed. R0 is a pointer -- it contains the address of data we’re interested in. x3107 x2819 x0110 x0310 x0100 x1110 x11B1 x0019 x3100 x3101 x3102 x3103 x3104 x3105 x3106 x3107 x3100 R0 address value 17- Another Need for Addresses Consider the following function that's supposed to swap the values of its arguments. void Swap(int firstVal, int secondVal) { int tempVal = firstVal; firstVal = secondVal; secondVal = tempVal; } 17- Executing the Swap Function valueA valueB firstVal secondVal tempVal 13 333 x4100 13 333 R6 before call valueA valueB firstVal secondVal tempVal 13 333 x4100 333 13 13 R6 after call These values changed. .but these did not. Swap needs addresses of variables outside its own activation record. 17- Pointers in C C lets us talk about and manipulate pointers as 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 z 17- Example int i; int *ptr; i = 4; ptr = &i; *ptr = *ptr + 1; store the value 4 into the memory location associated with i store the address of i into the memory location associated with ptr read | Chapter 17 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-2 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 address rather than its value Array A list of values arranged sequentially in memory Example: a list of telephone numbers Expression a[4] refers to the 5th element of the array a 17- Address vs. Value Sometimes we want to deal with the address of a memory location, rather than the value it contains. Recall example from Chapter 6: adding a column of numbers. R0 contains address of first location. Read value, add to sum, and increment R0 until all numbers have been processed. R0 is a pointer -- it contains the address of data we’re interested in. x3107 x2819 x0110 x0310 x0100 x1110 x11B1 x0019 x3100 x3101 x3102 x3103 x3104 x3105 x3106 x3107 x3100 R0 address value 17- Another Need for Addresses Consider the following function .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.