470 likes | 482 Views
Learn about pointers in programming - their definition, basics, usage, Address-Of and Dereferencing operators, and more in C++ programming language.
E N D
Pointer Definition: The Addressofa data object in memory or A variable whose data type and value are an Address ofa data object in memory.
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
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
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"
Address-Of Operator & In Memory:
Address Of Operator & "Hand Drawings"
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
Dereferencing Operator * inti=7, *p; p = &i; // p "points to" i *p = 9; // changes i to 9 "What is pointed to by p" = 9
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
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
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
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
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
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;
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;
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;
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.
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
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; }
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; }
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; }
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; }
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; }
What is the Data Type? class frac { public: intnum, int den }; void main() { frac f, *p;
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; }
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"
Pointers as PBV Arguments void fun(int * p) { *p = 7; } void main() { int x = 3; int *q = &x; fun(q); cout << x; }
Pointers as PBV Arguments void fun(int * p) { *p = 7; } void main() { int x = 3; int *q = &x; fun(q); cout << x; }
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; }
Pointers as PBV Arguments void fun(int * p) { *p = 7; } void main() { int x = 3; int *q = &x; fun(q); cout << x; }
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!
Pointers as PBV Arguments void doubleIt(int & y) { y = y * 2; } void main() { int x = 4; doubleIt(x); cout << x; // prints 8 }
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
Arrays Array Variables are Pointers!
Arrays int a[4]; ais actually type "pointer to int"
Arrays a[2] = 7; is translated as "2 int locations past a" = 7;
Array Arguments A Big Lie: "Array Arguments are always Pass by Reference"
Array Arguments The Truth: "Array Arguments are always Pass by Pointer"
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]; }
Array Arguments Example 3: void fun(int a[]) { a[2] = 7; } void main() { int b[4]; fun(b); cout << b[2]; }
Array Arguments Example 3: void fun(int a[]) { a[2] = 7; } void main() { int b[4]; fun(b); }
Array Arguments Example 3: void fun(int a[]) { a[2] = 7; } void main() { int b[4]; fun(b); cout << b[2]; }
Array Arguments Example 3: void fun(int a[]) { a[2] = 7; } void main() { int b[4]; fun(b); cout << b[2]; }
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!