Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.

Similar presentations


Presentation on theme: "Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer."— Presentation transcript:

1 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer in function call  Application: exchange values of two variables  Pointer and array

2 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-2 Pointer Introduction  Pointer definition:  Memory address of a variable  Recall: memory divided  Numbered memory locations  Variable name is used as address  You’ve used memory address already!  Call-by-reference  Address of actual argument was passed  Pass array in a function call

3 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-3 Pointer Variables  Pointers are "typed"  Can store addresses in variable  Pointers to int, double, etc.!  Example: double *p;  p is declared a "pointer to double" variable  Can hold pointers to variables of type double  Not other types!

4 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-4 Declaring Pointer Variables  Pointers declared like other types  Add "*" before variable name  Produces "pointer to" that type  "*" must be before each variable  int *p1, *p2, v1, v2;  p1, p2 hold pointers to int variables  v1, v2 are ordinary int variables

5 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-5 Pointing to …  int *p1, *p2, v1, v2; p1 = &v1;  Sets pointer variable p1 to "point to" int variable v1  Operator, &  Determines "address of" variable  Read like:  "p1 equals address of v1"  Or "p1 points to v1"

6 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-6 Pointing to …  Recall: int *p1, *p2, v1, v2; p1 = &v1;  Two ways to refer to v1 now:  Variable v1 itself: cout << v1;  Via pointer p1: cout << *p1;  Dereference operator, *  Pointer variable "dereferenced"  Means: "Get data that p1 points to"

7 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-7 "Pointing to" Example  Consider: v1 = 0; p1 = &v1; *p1 = 42; cout << v1 << endl; cout << *p1 << endl;  Produces output: 42 42  *p1 and v1 refer to same variable

8 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-8 & Operator  The "address of" operator  Also used to specify call-by-reference parameter  Similar mechanism  Call-by-reference parameters pass "address of" the actual argument

9 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-9 Pointer Assignments  Pointer variables can be "assigned": int *p1, *p2; p2 = p1;  Assigns one pointer to another  "Make p2 point to where p1 points"  Do not confuse with: *p1 = *p2;  Assigns "value pointed to" by p1, to "value pointed to" by p2

10 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-10 Pointer Assignments Graphic: Display 10.1 Uses of the Assignment Operator with Pointer Variables

11 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-11 Define Pointer Types  Can "name" pointer types  To be able to declare pointers like other variables  Eliminate need for "*" in pointer declaration  typedef int* IntPtr;  Defines a "new type" alias  Consider these declarations: IntPtr p; int *p;  The two are equivalent

12 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-12 Pointers and Functions  Recall  Pass by value  Pass by reference  Pointers can be function parameters  Behaves like pass by reference  Example: next slide

13 void main() { int m = 77; int * p = &m; cout << "m = " << m << endl; sneaky(p); cout << "m = " << m << endl; } void sneaky(int * temp) { *temp = 99; } Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-13

14 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-14 Call-by-value Pointers Graphic: The Function Call sneaky(p);

15 Application of Pointer  Exchange values of two variables  Pass by value: won’t work  Pass by reference: will work  Pass by pointer: will work  Pass by pointer to pointer: what is this? 10-15

16 Pointer and array  Both array and pointer are pass by reference in a function call  Both array and pointer refer to some physical memory address  So, in C++, pointers can be used to represent arrays  Any type of array (int, double, char, …) Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-16

17 Pointer and array (continued) double a[4] = {1.1, 2.2, 3.3, 4.4}; double f = 5.5; double *p; p = &f; p = a; // or p = & a[0]; // now p can be used same as a to access array for(int i=0; i<4; i++)cout << p[i] << "\t"; cout << endl; 10-17

18 Pointer and array (continued)  So, we can use pointer to represent array, even in a function call double a[4] = {1.1, 2.2, 3.3, 4.4}; double *p = a; reset(p, 4); //or reset(a, 4); for(int i=0; i<4; i++)cout << p[i] << "\t"; void reset(double * x, int size) { for(int i=0; i<size; i++)x[i]=5.5; } 10-18

19 Pointer and C-String  C-String is a character array, so pointer can be used to represent C-string  What will be displayed? char str[] = "cs201"; cout << str << endl; cout << "str contains " << strlen(str) << " characters" << endl; char *p; p = str; // p = &str[0]; cout << p << endl; cout << "p contains " << strlen(p) << " characters" << endl ; 10-19

20 Pointer and C-String (continued)  What should be displayed? char *p = "USA"; char *q = "Indiana"; q = p; reset(q); cout << "q=" << q << endl; void reset(char * x) { x="IU South Bend"; } 10-20

21 Pointer and Array and C-String  Assigment can not be performed in array but can be done in pointers int m[3] = {1, 2, 3}; int n[3] = {-1, -2, -3}; m = n; // This is NOT allowed int * p1 = m; int * p2 = n; p1 = p2; // This is allowed 10-21

22 Pointer and Array and C-String (continued)  Assigment can not be performed in C-string but can be done in pointers char m[] = “Hello”; char n[] = “South Bend”; m = n; // This is NOT allowed char * p1 = m; char * p2 = n; p1 = p2; // This is allowed 10-22


Download ppt "Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-1 Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer."

Similar presentations


Ads by Google