Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers: The Basics.

Similar presentations


Presentation on theme: "Pointers: The Basics."— Presentation transcript:

1 Pointers: The Basics

2 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.

3 Declaration datatype * identifier; Examples: int * ip; // ptr to int
float * fp; // ptr to float aPlayer * pp; // ptr to aPlayer object 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:
int i=7, j=9, *p, *q; p = &i; // p now "points to" i q = &j; // q now "points to" j

5 Address-Of Operator & If i is an int, then &i is "pointer to int"
Data Type: If i is an int, then &i is "pointer to int" int i=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: Address Value 2000 7 i 2001 9 j 2002
@2000 p 2003 @2001 q

7 Address Of Operator & "Hand Drawings"

8 Dereferencing Operator *
datatype id, *ptr; ptr = &id; *ptr = expressionOfDatatype; Example: int i=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 *
int i=7, *p; p = &i; // p "points to" i *p = 9; // changes i to 9 "What is pointed to by p" = 9

10 Example 1 int i=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 int i=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 int i=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 int i=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 int i=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 int i=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 int i=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 int j=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 constant NULL
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 class frac { public: int num, 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: int num, 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: int num, 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: int num, 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: int num, 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: int num, 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: int num, int den };
void main() { frac f, *p; Expr Data Type f frac f.den int p frac* (ptr to frac) (*p).den *p p->den &f &(f.den) int* (ptr to int) *f Syntax Error [SE]: f is not a ptr, so it cannot be dereferenced p.den SE: (p is not an object) *p.den SE: p.den is not a pointer

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 p and 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 Array Variables are Pointers!
Arrays Array Variables are Pointers!

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

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

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

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

41 Array Arguments Example 3: void fun(int a[]) {
//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 Term Definition Pointer
The Address Of a data object in memory; or a variable whose value is a memory address. Dereferencing Operator used on a pointer variable to refer to the object pointed to by the pointer (*). Address-Of Operator used on a data object to get a pointer to the object (&). NULL Pointer value commonly used by programmers to mean "empty" or "does not point to anything."


Download ppt "Pointers: The Basics."

Similar presentations


Ads by Google