Download presentation
Presentation is loading. Please wait.
1
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode. If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.
2
pointers Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 This lesson introduces the use of pointers: Vocabulary: address pointee pointer
3
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h The chart to the right represents computer memory. Each cell represents a single byte of memory. The addresses are written in hexadecimal to the left of the chart. The first cell in the top row is located at address 0000h The last cell in the top row is located at address 0003h The last cell in the bottom is located at address 001Bh The first few slides explain the diagrams that will be used in this slideshow. Don't fret about hexadecimal - you don't actually have to convert them. Just recognize that they are addresses in these diagrams.
4
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h Age When an int, float, char, or double is declared, the cell(s) will be green and the name of the variable will be written in very tiny letters in the lower left corner. Click for Example int Age; Click Tip Click Tip The green rectangle is 2 bytes wide because declaring an int reserves 2 bytes. char reserves 1 byte. float reserves 4 bytes. double reserves 8 bytes.
5
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h Age When a value is assigned to a variable, the value will be centered in the cell in a larger font than the name of the variable Click for Example Age = 23; Age 23
6
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h Click for Example int *pAge; Age 23 When pointer is declared, the cell will be blue and the name of the pointer will be written in very tiny letters in the lower left corner. pAge
7
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h Age When an address is assigned to a pointer, the value will be centered in the cell in a larger font than the name of the pointer. Click for Example pAge = &Age; Age 23 pAge 000C pAge
8
A pointer is a memory location that stores the address of a different memory location.The destination is called the pointee. 0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h pAge Age 000C pAge
9
The pointer “points” to the pointee. 0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h pAge Age 000C pAge
10
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h int *pAge; int Age; pAge Age When declaring a pointer, the data type given must be the data type of the pointee. So, if I’m defining a pointer that will point to an int, then I declare the pointer as int. When declaring a pointer, an asterisk, *, must be included either immediately after the data type or immediately before the name of the pointer. Notice that neither Age nor pAge contain a value at this point. They have been declared but have not been assigned a value.
11
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h int *pAge; int Age; pAge = &Age; pAge Age 000C pAge To store a value in pAge, assign it the address of a variable. Remember that the address of a scalar variable is expressed by placing an ampersand, &, in front of the variable name. Notice that I don't include the * when I assign an address to the pointer.
12
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h int *pAge; int Age; pAge = &Age; pAge Age 000C pAge There are 2 ways to store a value in Age. 1: The most common way is by using the assignment operator on the variable to make a direct assignment. Age = 23; Age 23
13
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h int *pAge; int Age; pAge = &Age; pAge Age 000C pAge There are 2 ways to store a value in Age. 2: An alternate method is to way is by using the assignment operator and the dereference operator on the pointer to make an indirect assignment. Age = 23; Age 30 *pAge = 30;
14
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h int *pAge; int Age; pAge = &Age; pAge 000C pAge When assigning a value to a variable via the pointer, the computer first looks up the address in the pointer and then moves to that address to store the value. Age = 23; *pAge = 30; Age 23 Step 1: Find pAge. Step 3: Go to the address. Step 2: Read the address in pAge. Step 4: Write the value into that memory address. Age 30
15
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h *pSalary = 54000.00; 2130 float *pSalary; float Salary; pSalary = &Salary; Another example. In this case, we will work with a float so the actual float variable will take up 4 bytes. pSalary 0014 pSalary Age 30 Salary 000C pAge 54000.00 Salary
16
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h *pSalary = 54000.00; 2130 float *pSalary; float Salary; pSalary = &Salary; pSalary 0014 pSalary Age 30 Salary 000C pAge 54000.00 Salary Notice: The * (dereference operator) is used when declaring the pointer and when I assigning a value in the pointee. The * (dereference operator) is NOT used when assigning an address to the pointer.
17
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h *pSalary = 54000.00; 2130 float *pSalary; float Salary; pSalary = &Salary; pSalary 0014 pSalary Age 30 Salary 000C pAge 54000.00 Salary Another way to remember when to use the dereference operator (*) If storing a value (address) here, don't use the *. You don't want to redirect the storage to a different location. You want the address stored in the pointer location itself! If storing a value (real content) here, use the *. You do want to redirect (dereference) the storage to a location other than the cell that contains the pointer.
18
WHY BOTHER ???? You are probably asking a question....
19
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h 000C h 0014 h 2130 54000.00 One of the most common uses of pointers is to pass the location of local variables between functions. void Fort(void);
20
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h 000C h 0014 h 2130 54000.00 When processing is outside of the local function, the variables local to the function cannot be referenced by name. void Fort(void); Oh, heck. I can't remember the variables
21
0000-0003 h 0004-0007 h 0008-000B h 000C-000F h 0010-0013 h 0014-0017 h 0018-001B h 000C h 0014 h 2130 54000.00 BUT...the memory location can be accessed if the address is known! void Fort(&Age); Store 18 in 000C h
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.