1 / 24

Slide 1 of 36

Slide 1 of 36. Data type Actual value stored in 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 <iostream> using namespace std; int main() { int num; num = 22;

marvin
Download Presentation

Slide 1 of 36

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. Slide 1 of 36

  2. Data type Actual value stored in 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 <iostream> using namespace std; int main() { int num; num = 22; cout << "num = " << num << endl; cout << "The address of ” << “ num = " << &num << endl; return 0; } Attributes Associated with Every Variable

  3. Pointers are Variables that Store Addresses of Other Variables Pointers must be declared: Use an indirection operator * Specify the data type that is pointed to Examples: int *numAddr; float *tabPoint; char *charPoint; 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 • Advantage – hides a lot of internal pointer manipulations from programmer However, pointers can be added, incremented, and are used by many advanced programmers Reference Notation Pointer Notation int b; int b; int &a = b; int *a = &b; 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 <iostream> // 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 • 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. • Array names are (constant) pointers that contain the address of the FIRST array element, Array[0]

  8. wierdPtr cef4 cef4 normal 99 Indirection Operator • *ptr a two-step lookup, i. e. the variable whose address is stored in ptr • A double lookup • get the value of ptr • use that value to look up value located in memory int normal = 99; int *weirdPtr = &normal; cout << normal << ‘ ‘ << *weirdPtr; Output 99 99

  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 • float* fptr; // pointer to float • char * cptr; // pointer to char • 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 <iostream> 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 • The pointer is “anonymous” or hidden • Array name is a constant pointer • Individual element access • Calculate offset for this element (how many bytes?) • Add to address of first element • Treat like any other pointer, except constant • Pointer to an array element can be treated like an array: ptr[index] or use pointer arithmetic

  12. Two ways to Reference Array Elements

  13. Program 9-5, a pointer version of 9-4See pages 419 - 422 // Program 9-5 - pointer access to an array #include <iostream> using namespace std; int main() { int *gPtr; // declare a pointer to an int const int SIZE = 5; int i, grade[SIZE] = {98, 87, 92, 79, 85}; gPtr = &grade[0]; // store 1st element address for (i = 0; i < SIZE; i++) cout << "Element " << i << " is " << *(gPtr + i) << endl; return 0; }

  14. Program 9 - 6 // Since array name is CONSTANT pointer, // just use array name with pointer notation #include <iostream> using namespace std; int main() { const int SIZE = 5; int i, grade[SIZE] = {98, 87, 92, 79, 85}; for (i = 0; i < SIZE; i++) cout << "Element " << i << " is " << *(grade + i) << endl; return 0; }

  15. Slide 21 of 36

  16. Pointers: What’s the Point? • Used extensively with strings • Used for call-by-reference • Early form of generic variables • Since 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 • Used 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) • This allocation uses the program stack – a temporary location • Static variables remain allocated and retain value. They are placed in the (permanent) heap. • Dynamically allocated memory comes from the heap • new obtains a block of storage • delete returns it for later use • 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. 3 4 Slide 30 of 36

  20. Slide 28 of 36

  21. Increments are Scaled when used with Pointers • If ptr is a pointer to an integer… • ptr++ causes the pointer to point to the next integer • ptr-- causes the pointer to point to the previous integer • ptr++ or ptr-- actually moves four bytes • If ptr is a pointer to a double… • ptr++ causes the pointer to point to the next double • ptr-- causes the pointer to point to the previous double • ptr++ or ptr-- actually moves eight bytes • If ptr is a pointer to a character… • ptr++ 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 = &num; // 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

More Related