Download presentation
Presentation is loading. Please wait.
Published byChester Morrison Modified over 9 years ago
1
Slide 1 of 36
2
Attributes Associated with Every Variable Data type Data type Actual value stored in variable Actual value stored in variable Address of variable Address of variable Study Programs 9-1 and 9-2 9.1 also uses sizeof function to display number of bytes of storage used #include #include using namespace std; int main() { int num; int num; num = 22; num = 22; cout << "num = " << num << endl; cout << "num = " << num << endl; cout << "The address of ” << “ num = " cout << "The address of ” << “ num = " << &num << endl; return 0; }
3
Pointers are Variables that Store Addresses of Other Variables Pointers must be declared: Pointers must be declared: Use an indirection operator *Use an indirection operator * Specify the data type that is pointed toSpecify the data type that is pointed to Examples: Examples: int *numAddr;int *numAddr; float *tabPoint;float *tabPoint; char *charPoint;char *charPoint; OS must know OS must know how many bytes to set aside for the variable pointed to The variable pointed to by numAddr is an integer The variable pointed to by tabPoint is a float The variable pointed to by charPoint is a character
4
Which to Use? References or Pointers? A reference is a pointer with restricted capabilities A reference is a pointer with restricted capabilities Advantage – hides a lot of internal pointer manipulations from programmerAdvantage – hides a lot of internal pointer manipulations from programmer However, pointers can be added, incremented, and are used by many advanced programmers Reference NotationPointer Notation int b; int b;int b; int &a = b; int &a = b; int *a = &b; a = 10; a = 10; *a = 10; (See pnt.cc) a is a reference variable that stores b’s address. a is not a pointer and can store other values
5
#include // Program 9-3 Page 412 using namespace std: int main() { int *numAddr; // declare a pointer to an int int miles, dist; // declare two integer variables dist = 158; // store the number 158 into dist miles = 22; // store the number 22 into miles numAddr = &miles; // store the 'address of miles' in numAddr cout << "\nThe address stored in numAddr is " << numAddr << endl; cout << "The value pointed to by numAddr is " << *numAddr << endl; numAddr = &dist; // now store the address of dist in numAddr cout << "\nThe address now stored in numAddr is " << numAddr << endl; cout << "The value now pointed to by numAddr is " << *numAddr << endl; return 0; }
6
Slide 6 of 36
7
Saving an address Addresses may only be stored in variables of type pointer Addresses may only be stored in variables of type pointer All pointer variables take the same amount of memory, 4 bytes in our system All pointer variables take the same amount of memory, 4 bytes in our system Pointer variables have types: pointer to int, pointer to char, etc. Pointer variables have types: pointer to int, pointer to char, etc. Array names are (constant) pointers that contain the address of the FIRST array element, Array[0] Array names are (constant) pointers that contain the address of the FIRST array element, Array[0]
8
Indirection Operator *ptr a two-step lookup, i. e. the variable whose address is stored in ptr *ptr a two-step lookup, i. e. the variable whose address is stored in ptr A double lookup A double lookup get the value of ptrget the value of ptr use that value to look up value located in memoryuse that value to look up value located in memory int normal = 99; int *weirdPtr = &normal; cout << normal << ‘ ‘ << *weirdPtr; Output 99 wierdPtr cef4 normal 99 cef4
9
More on Declaring Pointers int *num1, num2; // pointer to an int and an int int *num1, num2; // pointer to an int and an int int * num1, * num2; // two pointers to int int * num1, * num2; // two pointers to int float* fptr;// pointer to float float* fptr;// pointer to float char * cptr; // pointer to char char * cptr; // pointer to char Type of thing pointed to is used for cin, cout, arithmetic, and comparisons: cout << *fptr; Type of thing pointed to is used for cin, cout, arithmetic, and comparisons: cout << *fptr; When used in an expression *fptr is a float, *cptr is a character, and *num1 is an integer
10
// Pointer version of findMax #include using namespace std; main () { // cin makes more sense here int n1 = 10, n2 = 5, n3 = 21, n4 = 17, n5 = 33; int* big = &n1; // remember address of n1; n1 is the //biggest so far if (n2 > *big) // Is 5 > 10? big = &n2; // no change if (n3 > *big) // Is 21 > 10? big = &n3; // big <- address of n3 if (n4 > *big) // Is 17 > 21? big = &n4; // no change if (n5 > *big) // Is 33 > 21? big = &n5; // big <- address of n5 cout << *big << " is biggest.\n"; *big = *big * 10; // pointer notation in an expression cout << *big << " is biggest multiplied by 10.\n";}
11
Array Names as Pointers Every array is implemented with a pointer to the actual data area Every array is implemented with a pointer to the actual data area The pointer is “anonymous” or hiddenThe pointer is “anonymous” or hidden Array name is a constant pointerArray name is a constant pointer Individual element access Individual element access Calculate offset for this element (how many bytes?)Calculate offset for this element (how many bytes?) Add to address of first elementAdd to address of first element Treat like any other pointer, except constant Treat like any other pointer, except constant Pointer to an array element can be treated like an array: ptr[index] or use pointer arithmetic Pointer to an array element can be treated like an array: ptr[index] or use pointer arithmetic
12
Two ways to Reference Array Elements Array Element Subscript Notation Pointer Notation Element 0 grade[0] *gradePtr *gradePtr Element 1 grade[1] *(gradePtr +1) Element 2 grade[2] *(gradePtr +2) Element 3 grade[3] *(gradePtr +3) Element 4 grade[4] *(gradePtr +4)
13
Program 9-5, a pointer version of 9-4 See pages 419 - 422 // Program 9-5 - pointer access to an array #include using namespace std; int main() { int *gPtr; // declare a pointer to an int int *gPtr; // declare a pointer to an int const int SIZE = 5; const int SIZE = 5; int i, grade[SIZE] = {98, 87, 92, 79, 85}; int i, grade[SIZE] = {98, 87, 92, 79, 85}; gPtr = &grade[0]; // store 1 st element address gPtr = &grade[0]; // store 1 st element address for (i = 0; i < SIZE; i++) for (i = 0; i < SIZE; i++) cout << "Element " << i << " is " cout << "Element " << i << " is " << *(gPtr + i) << endl; return 0; return 0;}
14
Program 9 - 6 // Since array name is CONSTANT pointer, // just use array name with pointer notation // Since array name is CONSTANT pointer, // just use array name with pointer notation #include using namespace std; int main() { const int SIZE = 5; const int SIZE = 5; int i, grade[SIZE] = {98, 87, 92, 79, 85}; int i, grade[SIZE] = {98, 87, 92, 79, 85}; for (i = 0; i < SIZE; i++) for (i = 0; i < SIZE; i++) cout << "Element " << i << " is " cout << "Element " << i << " is " << *(grade + i) << endl; << *(grade + i) << endl; return 0; return 0;}
15
Slide 21 of 36
16
Pointers: What’s the Point? Used extensively with strings Used extensively with strings Used for call-by-reference Used for call-by-reference Early form of generic variables Early form of generic variables Since pointers are all the same size, a pointer variable is capable of pointing to any data typeSince pointers are all the same size, a pointer variable is capable of pointing to any data type Allows the programmer to keep track of dynamically allocated memory Allows the programmer to keep track of dynamically allocated memory Used extensively in Data Structures courseUsed extensively in Data Structures course
17
Memory Management Most of the variables we have used have been automatic, i. e., they are allocated memory when the function begins and destroyed when it ends (Includes globals) Most of the variables we have used have been automatic, i. e., they are allocated memory when the function begins and destroyed when it ends (Includes globals) This allocation uses the program stack – a temporary locationThis allocation uses the program stack – a temporary location Static variables remain allocated and retain value. They are placed in the (permanent) heap. Static variables remain allocated and retain value. They are placed in the (permanent) heap. Dynamically allocated memory comes from the heap Dynamically allocated memory comes from the heap new obtains a block of storagenew obtains a block of storage delete returns it for later usedelete returns it for later use Program 9-7 is an example of new and delete. Linked lists, Section 3 - Chapter 11, is better example Program 9-7 is an example of new and delete. Linked lists, Section 3 - Chapter 11, is better example
18
Slide 25 of 36
19
Slide 30 of 36 34
20
Slide 28 of 36
21
Increments are Scaled when used with Pointers If ptr is a pointer to an integer… If ptr is a pointer to an integer… ptr++ causes the pointer to point to the next integerptr++ causes the pointer to point to the next integer ptr-- causes the pointer to point to the previous integerptr-- causes the pointer to point to the previous integer ptr++ or ptr-- actually moves four bytesptr++ or ptr-- actually moves four bytes If ptr is a pointer to a double… If ptr is a pointer to a double… ptr++ causes the pointer to point to the next doubleptr++ causes the pointer to point to the next double ptr-- causes the pointer to point to the previous doubleptr-- causes the pointer to point to the previous double ptr++ or ptr-- actually moves eight bytesptr++ or ptr-- actually moves eight bytes If ptr is a pointer to a character… If ptr is a pointer to a character… ptr++ or ptr-- moves ONE byteptr++ or ptr-- moves ONE byte
22
Slide 31 of 36
23
KNOW THIS! Pointers behave differently with numbers than with character arrays /* Pointers to numbers vs. */ /* Pointers to character strings */ /* What is accessed or displayed? */ /* */ /* Pointer to With * Without * */ /* */ /* int/float int/float address */ /* */ /* char char BYTE string from there*/ Look at numsVSchars.cc
24
Common Errors int location = # // location is not a pointer variable, no* (Use descriptive variable names: numAddr, fPtr, chPtr, ptr…) Int * ptr1, ptr2;// without *, ptr2 is not a pointer int nums[3], *ptr; Use ptr = nums; or ptr = &nums[0]; NOT ptr = &nums; // nums is already a const pointer int *ptr = &99;// can’t take address of literal Int *ptr = &(diameter * PI); // nor of an expression
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.