1 / 47

Pointers: The Basics

Pointers: The Basics. Pointer. Definition: The Address of a data object in memory or A variable whose data type and value are an Address of a data object in memory. Declaration. datatype * identifier ; Examples: int * ip ; // ptr to int float * fp ; // ptr to float

layton
Download Presentation

Pointers: The Basics

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. Pointers: The Basics

  2. Pointer Definition: The Addressofa data object in memory or A variable whose data type and value are an Address ofa data object in memory.

  3. Declaration datatype * identifier; Examples: int * ip; // ptr to int float * fp; // ptr to float aPlayer * pp; // ptr to aPlayerobject int *p, *q, *r; // 3 ptrs to int int *p, q, r; // 1 ptr, 2 ints

  4. Address-Of Operator & datatype id, *ptr; ptr=& id; Example: inti=7, j=9, *p, *q; p = &i; // p now "points to" i q = &j; // q now "points to" j

  5. Address-Of Operator & Data Type: If iis an int, then &iis "pointer to int" inti=7, j=9, *p, *q; p = &i; // "ptr to int" = "ptr to int" q = &j; // "ptr to int" = "ptr to int"

  6. Address-Of Operator & In Memory:

  7. Address Of Operator & "Hand Drawings"

  8. Dereferencing Operator * datatype id, *ptr; ptr= &id; *ptr= expressionOfDatatype; Example: inti=7, *p; p = &i; // p now "points to" i *p = 9; // changes i to 9 "What is pointed to by p" = 9

  9. Dereferencing Operator * inti=7, *p; p = &i; // p "points to" i *p = 9; // changes i to 9 "What is pointed to by p" = 9

  10. Example 1 inti=7, int j=9, *p; p = &i; // p "points to" i *p = 5; // changes i to 5 p = &j; // p "points to" j *p = 3; // changes j to 3

  11. Example 1 inti=7, int j=9, *p; p = &i; // p "points to" i *p = 5; // changes i to 5 p = &j; // p "points to" j *p = 3; // changes j to 3

  12. Example 1 inti=7, int j=9, *p; p = &i; // p "points to" i *p = 5; // changes i to 5 p = &j; // p "points to" j *p = 3; // changes j to 3

  13. Example 1 inti=7, int j=9, *p; p = &i; // p "points to" i *p = 5; // changes i to 5 p = &j; // p "points to" j *p = 3; // changes j to 3

  14. Example 1 inti=7, int j=9, *p; p = &i; // p "points to" i *p = 5; // changes i to 5 p = &j; // p "points to" j *p = 3; // changes j to 3

  15. Example 1 inti=7, int j=9, *p; p = &i; // p "points to" i *p = 5; // changes i to 5 p = &j; // p "points to" j *p = 3; // changes j to 3 cout << *p << " " << i << " " << j;

  16. Example 1 inti=7, int j=9, *p; p = &i; // p "points to" i *p = 5; // changes i to 5 p = &j; // p "points to" j *p = 3; // changes j to 3 cout << *p << " " << i << " " << j;

  17. Copying a Pointer intj=7, *p, *q; p = &j; // p "points to" j q = p; // make q point to // the same thing as p *p = 6; // changes j to 6 *q = 5; // changes j to 5 cout << *p << " " << *q << " "<< i;

  18. NULL #include <iostream> defines the global constantNULL NULL is commonly used to mean "empty" or "points to nothing" However, it in fact is simply Address 0000, and dereferencing it causes a run time error.

  19. NULL

  20. Pointers to Objects classfrac { public: intnum, int den }; void main() { fract f; // a fraction fract *p; // ptr to a fraction p = &f; // p points to f

  21. Arrow Operator -> class frac { public: intnum, int den }; void main() { fract f; // a fraction fract *p; // ptr to a fraction p = &f; // p points to f *p.num = 1;// Syntax error (*p).num = 1;// OK, but cumbersome p->num = 1; // Same semantics p->den = 2; }

  22. Example 2: class frac { public: intnum, int den }; void main() { fract f; fract*p = &f; f.num = 2; f.den = 3; p->num = 1; p->den = 2; cout << f.num << " " << f.den; }

  23. Example 2: class frac { public: intnum, int den }; void main() { fract f; fract*p = &f; f.num = 2; f.den = 3; p->num = 1; p->den = 2; cout << f.num << " " << f.den; }

  24. Example 2: class frac { public: intnum, int den }; void main() { fract f; fract*p = &f; f.num = 2; f.den = 3; p->num = 1; p->den = 2; cout << f.num << " " << f.den; }

  25. Example 2: class frac { public: intnum, int den }; void main() { fract f; fract*p = &f; f.num = 2; f.den = 3; p->num = 1; p->den = 2; cout << f.num << " " << f.den; }

  26. What is the Data Type? class frac { public: intnum, int den }; void main() { frac f, *p;

  27. Pointers as PBV Arguments Nothing new...except the Implications void fun(int * p) { *p = 7; } void main() { int x = 3; int *q = &x; fun(q); cout << x; }

  28. Pointers as PBV Arguments void fun(int * p) { ... fun(q); - p is allocated - a COPY of q is assigned to p, which means "p points to the same thing as q" Note: Arguments are of type "pointer to int"

  29. Pointers as PBV Arguments void fun(int * p) { *p = 7; } void main() { int x = 3; int *q = &x; fun(q); cout << x; }

  30. Pointers as PBV Arguments void fun(int * p) { *p = 7; } void main() { int x = 3; int *q = &x; fun(q); cout << x; }

  31. Pointers as PBV Arguments void fun(int * p) { *p = 7; } // deallocate pand return void main() { int x = 3; int *q = &x; fun(q); cout << x; }

  32. Pointers as PBV Arguments void fun(int * p) { *p = 7; } void main() { int x = 3; int *q = &x; fun(q); cout << x; }

  33. Pointers as PBV Arguments Implication: Though a Formal Pointer Argument is semantically PBV, Changing the value of the object pointed to has the Same effect as "standard" PBR!

  34. Pointers as PBV Arguments void doubleIt(int & y) { y = y * 2; } void main() { int x = 4; doubleIt(x); cout << x; // prints 8 }

  35. Pointers as PBV Arguments void doubleIt(int * y) { *y = *y * 2; } void main() { int x = 4; doubleIt(&x); cout << x; // prints 8 } This is called Pass By Pointer

  36. Arrays Array Variables are Pointers!

  37. Arrays int a[4]; ais actually type "pointer to int"

  38. Arrays a[2] = 7; is translated as "2 int locations past a" = 7;

  39. Array Arguments A Big Lie: "Array Arguments are always Pass by Reference"

  40. Array Arguments The Truth: "Array Arguments are always Pass by Pointer"

  41. Array Arguments Example 3: void fun(inta[]) { //void fun(int * a) { // same meaning a[2] = 7; } void main() { int b[4]; //b is actually "ptr to int" fun(b); cout << b[2]; }

  42. Array Arguments Example 3: void fun(int a[]) { a[2] = 7; } void main() { int b[4]; fun(b); cout << b[2]; }

  43. Array Arguments Example 3: void fun(int a[]) { a[2] = 7; } void main() { int b[4]; fun(b); }

  44. Array Arguments Example 3: void fun(int a[]) { a[2] = 7; } void main() { int b[4]; fun(b); cout << b[2]; }

  45. Array Arguments Example 3: void fun(int a[]) { a[2] = 7; } void main() { int b[4]; fun(b); cout << b[2]; }

  46. Usefulness of Pointers What good are they? So far, very little: - Pass by Pointer as an alternative to Pass by Reference (needed in C) - Clearer understanding of Arrays But there is more to come: Dynamic Memory Management!

  47. Vocabulary

More Related