1 / 21

This presentation includes custom animations.

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.

gypsy
Download Presentation

This presentation includes custom animations.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 This lesson introduces the use of pointers: Vocabulary: address pointee pointer Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  3. 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.

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. The pointer “points” to the pointee. pAge 0000-0003h 0004-0007h 0008-000Bh 000C pAge 000C-000Fh Age 0010-0013h 0014-0017h 0018-001Bh

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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.

  15. 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

  16. 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

  17. 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

  18. You are probably asking a question.... WHY BOTHER ????

  19. 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

  20. 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

  21. 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

More Related