Upload
gypsy
View
36
Download
0
Tags:
Embed Size (px)
DESCRIPTION
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. - PowerPoint PPT Presentation
Citation preview
This presentation includes custom animations.
To view the animations, you must view the presentation in Slide Show modeand 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
Christine S. WolfeOhio University Lancaster2008-Aug-01
This lesson introduces the use of pointers:
Vocabulary:
addresspointeepointer
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
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.
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
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;
ClickTip
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.
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
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
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
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
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
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
pAge000CpAge
A pointer is a memory location that stores the address of a different memory location.The destination is called the pointee.
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
pAge
Age
000CpAge
The pointer “points” to the pointee.
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
pAge
Age
000CpAge
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
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.
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
int *pAge;
int Age;
pAge = &Age;
pAge
Age
000CpAge
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.
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
int *pAge;
int Age;
pAge = &Age;
pAge
Age
000CpAge
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
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
int *pAge;
int Age;
pAge = &Age;
pAge
Age
000CpAge
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;
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
int *pAge;
int Age;
pAge = &Age;
pAge000CpAge
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
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
*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.
pSalary0014
pSalary
Age 30
Salary
000CpAge
54000.00Salary
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
*pSalary = 54000.00;2130
float *pSalary;
float Salary;pSalary = &Salary;
pSalary0014
pSalary
Age 30
Salary
000CpAge
54000.00Salary
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.
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
*pSalary = 54000.00;2130
float *pSalary;
float Salary;pSalary = &Salary;
pSalary0014
pSalary
Age 30
Salary
000CpAge
54000.00Salary
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.
WHY BOTHER
????
You are probably asking a question....
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
000Ch 0014h
2130
54000.00
One of the most common uses of pointers is to pass the location of local variables between functions.
void Fort(void);
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
000Ch 0014h
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
0000-0003h
0004-0007h
0008-000Bh
000C-000Fh
0010-0013h
0014-0017h
0018-001Bh
000Ch 0014h
2130
54000.00
BUT...the memory location can be accessed if the address is known!
void Fort(&Age);
Store 18 in 000Ch