Operator The "address of"operator Also used to specify call-by-reference parameter ◆No coincidence! Recall:call-by-reference parameters pass "address of"the actual argument Operator's two uses are closely related Copyright006 Pearson Addison-Wesley.All rights reserved. 10-11
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-11 & Operator ¨ The "address of" operator ¨ Also used to specify call-by-reference parameter ¨No coincidence! ¨Recall: call-by-reference parameters pass "address of" the actual argument ¨ Operator’s two uses are closely related
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 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 10-12
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-12 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
Pointer Assignments Graphic: Display 10.1 Uses of the Assignment Operator with Pointer Variables Display 1o.1 Uses of the Assignment Operator with Pointer Variables p1=p2; Before: After: p1 8 p1 8 9 *p1=*p2; Before: After: p2 9 p2 9 Copyright006 Pearson Addison-Wesley.All rights reserved. 10-13
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-13 Pointer Assignments Graphic: Display 10.1 Uses of the Assignment Operator with Pointer Variables
The new Operator Since pointers can refer to variables. No "real"need to have a standard identifier Can dynamically allocate variables Operator new creates variables No identifiers to refer to them ◆Just a pointer! ◆p1=new int;: Creates new "nameless"variable,and assigns p1 to "point to"it ◆Can access with*p1 Use just like ordinary variable Copyright 2006 Pearson Addison-Wesley.All rights reserved. 10-14
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-14 The new Operator ¨ Since pointers can refer to variables. ¨ No "real" need to have a standard identifier ¨ Can dynamically allocate variables ¨ Operator new creates variables ¨ No identifiers to refer to them ¨ Just a pointer! ¨ p1 = new int; ¨ Creates new "nameless" variable, and assigns p1 to "point to" it ¨ Can access with *p1 ¨ Use just like ordinary variable
Basic Pointer Manipulations Example: Display 10.2 Basic Pointer Manipulations (1 of 2) Display 1o.2 Basic Pointer Manipulations //Program to demonstrate pointers and dynamic variables. #include <iostream> using std:cout; 4 using std:endl; 5 int main() 6 7 int *pl,*p2; pl new int; 9 *p1=42; p2=p1; 1 cout <"*pl =="<<*p1 <endl; 12 cout <<"*p2 =="<*p2 <endl; 1 *p2=53; 4 cout <"*pl =="<<*pl <endl; 15 cout <<"*p2 =="<<*p2 <endl; Copyright006 Pearson Addison-Wesley.All rights reserved. 10-15
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-15 Basic Pointer Manipulations Example: Display 10.2 Basic Pointer Manipulations (1 of 2)