210 likes | 334 Views
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.
E N D
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.
pointers This lesson introduces the use of pointers: Vocabulary: address pointee pointer Christine S. Wolfe Ohio University Lancaster 2008-Aug-01
The first few slides explain the diagrams that will be used in this slideshow. • 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 0000-0003h 0004-0007h 0008-000Bh 000C-000Fh 0010-0013h 0014-0017h 0018-001Bh Don't fret about hexadecimal - you don't actually have to convert them. Just recognize that they are addresses in these diagrams.
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; 0000-0003h 0004-0007h The green rectangle is 2 bytes wide because declaring an int reserves 2 bytes. 0008-000Bh 000C-000Fh Age 0010-0013h Click Tip char reserves 1 byte. float reserves 4 bytes. double reserves 8 bytes. 0014-0017h 0018-001Bh
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; 0000-0003h 0004-0007h 0008-000Bh 000C-000Fh Age • 23 Age 0010-0013h 0014-0017h 0018-001Bh
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 Click for Example int *pAge; 0000-0003h 0004-0007h 0008-000Bh 000C-000Fh • 23 Age 0010-0013h 0014-0017h 0018-001Bh
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. pAge Click for Example pAge = &Age; 0000-0003h 0004-0007h 0008-000Bh 000C pAge 000C-000Fh Age Age • 23 0010-0013h 0014-0017h 0018-001Bh
A pointer is a memory location that stores the address of a different memory location.The destination is called the pointee. pAge 0000-0003h 0004-0007h 0008-000Bh 000C pAge 000C-000Fh Age 0010-0013h 0014-0017h 0018-001Bh
The pointer “points” to the pointee. pAge 0000-0003h 0004-0007h 0008-000Bh 000C pAge 000C-000Fh Age 0010-0013h 0014-0017h 0018-001Bh
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. pAge int Age; 0000-0003h int *pAge; 0004-0007h 0008-000Bh 000C-000Fh Age Notice that neither Age nor pAge contain a value at this point. They have been declared but have not been assigned a value. 0010-0013h 0014-0017h 0018-001Bh
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. pAge int Age; 0000-0003h 000C int *pAge; pAge 0004-0007h pAge= &Age; 0008-000Bh 000C-000Fh Age 0010-0013h Notice that I don't include the * when I assign an address to the pointer. 0014-0017h 0018-001Bh
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. pAge int Age; 0000-0003h 000C int *pAge; pAge 0004-0007h pAge= &Age; 0008-000Bh Age = 23; 000C-000Fh Age • 23 Age 0010-0013h 0014-0017h 0018-001Bh
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. pAge int Age; 0000-0003h 000C int *pAge; pAge 0004-0007h pAge= &Age; 0008-000Bh Age = 23; *pAge= 30; 000C-000Fh Age • 30 Age 0010-0013h 0014-0017h 0018-001Bh
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. pAge int Age; 0000-0003h 000C int *pAge; pAge 0004-0007h pAge= &Age; 0008-000Bh Age = 23; *pAge= 30; 000C-000Fh • 23 • 30 Age Age 0010-0013h Step 1: Find pAge. 0014-0017h Step 2: Read the address in pAge. 0018-001Bh Step 3: Go to the address. Step 4: Write the value into that memory address.
Another example. In this case, we will work with a float so the actual float variable will take up 4 bytes. 000C pSalary 0014 pAge pSalary 0000-0003h float *pSalary; float Salary; 0004-0007h pSalary= &Salary; 0008-000Bh *pSalary= 54000.00; 000C-000Fh 30 21 • 30 Age 0010-0013h 0014-0017h 0018-001Bh 54000.00 Salary 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. 000C pSalary 0014 pAge pSalary 0000-0003h float *pSalary; float Salary; 0004-0007h pSalary= &Salary; 0008-000Bh *pSalary= 54000.00; 000C-000Fh 21 30 • 30 Age 0010-0013h 0014-0017h 0018-001Bh 54000.00 Salary 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! 000C pSalary 0014 pAge pSalary 0000-0003h float *pSalary; float Salary; 0004-0007h pSalary= &Salary; 0008-000Bh *pSalary= 54000.00; 000C-000Fh 21 30 • 30 Age 0010-0013h 0014-0017h 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. 0018-001Bh 54000.00 Salary Salary
You are probably asking a question.... WHY BOTHER ????
One of the most common uses of pointers is to pass the location of local variables between functions. void Fort(void); 0000-0003h 000Ch 0014h 0004-0007h 0008-000Bh 000C-000Fh 21 30 0010-0013h 0014-0017h 54000.00 0018-001Bh
When processing is outside of the local function, the variables local to the function cannot be referenced by name. Oh, heck. I can't remember the variables void Fort(void); 0000-0003h 000Ch 0014h 0004-0007h 0008-000Bh 000C-000Fh 21 30 0010-0013h 0014-0017h 54000.00 0018-001Bh
BUT...the memory location can be accessed if the address is known! void Fort(&Age); Store 18 in 000Ch 0000-0003h 000Ch 0014h 0004-0007h 0008-000Bh 000C-000Fh 21 30 0010-0013h 0014-0017h 54000.00 0018-001Bh