Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Overview of References and Pointers <ul><li>Often need to  refer  to another object </li></ul><ul><ul><li>Without making a...
Untangling Operator Syntax Symbol Used in a declaration Used in a definition unary  &  (ampersand) reference int i = 3; in...
What’s a Reference in C++? <ul><li>A variable holding an address </li></ul><ul><ul><li>Of what it “refers to” in memory </...
Const References <ul><li>int main (int, char *[]) { </li></ul><ul><li>const int i = 0; </li></ul><ul><li>int j = 1; </li><...
Exercise <ul><li>Write a simple main function </li></ul><ul><ul><li>or just use the one provided in the  array_exercise0.c...
What’s a Pointer in C++? <ul><li>A variable holding an address </li></ul><ul><ul><li>Of what it “points to” in memory </li...
Location and Value Comparisons <ul><li>Pointers may be compared for equality </li></ul><ul><ul><li>Same as comparing addre...
Const Pointers and Pointers to Const Types <ul><li>int main (int, char *[]) { </li></ul><ul><li>const int i = 0; </li></ul...
Exercise <ul><li>Write another simple main function </li></ul><ul><li>Declare two integers  </li></ul><ul><ul><li>Give the...
Upcoming SlideShare
Loading in …5
×

C++ Pointers And References

1,054 views

Published on

Published in: Technology
  • Be the first to comment

C++ Pointers And References

  1. 1. Overview of References and Pointers <ul><li>Often need to refer to another object </li></ul><ul><ul><li>Without making a copy of the object itself </li></ul></ul><ul><ul><li>Also known as “aliasing” </li></ul></ul><ul><li>Two ways to do this </li></ul><ul><ul><li>Directly, via a reference </li></ul></ul><ul><ul><ul><li>Acts as an alias for the object </li></ul></ul></ul><ul><ul><ul><li>User interacts with reference as if it were the object itself </li></ul></ul></ul><ul><ul><li>Indirectly, via a pointer </li></ul></ul><ul><ul><ul><li>Gives the address (in memory) of the object </li></ul></ul></ul><ul><ul><ul><li>Requires the user to do extra work: dereferencing </li></ul></ul></ul>
  2. 2. Untangling Operator Syntax Symbol Used in a declaration Used in a definition unary & (ampersand) reference int i = 3; int &r = i; address-of p = &i; unary * (star) pointer int * p; dereference (get what’s pointed to) *p = 7; -> (arrow) member access via pointer cp->add(3); . (dot) member access (same syntax for either reference or object) c.add(3); [] (square bracket) array dimensions int a[3]; array indexing cout << a[0] << endl;
  3. 3. What’s a Reference in C++? <ul><li>A variable holding an address </li></ul><ul><ul><li>Of what it “refers to” in memory </li></ul></ul><ul><li>But with a nicer interface </li></ul><ul><ul><li>An alias to the object </li></ul></ul><ul><ul><li>Hides indirection from programmer </li></ul></ul><ul><li>Must be typed </li></ul><ul><ul><li>Checked by compiler </li></ul></ul><ul><ul><li>Again can only refer to the type to which it can point </li></ul></ul><ul><ul><li>int &r = i; // can only refer to int </li></ul></ul><ul><li>Must always refer to something </li></ul><ul><ul><li>Must be initialized, cannot be changed </li></ul></ul><ul><ul><li>More restricted than Java references </li></ul></ul>0x7fffdad0 7 int i int &r
  4. 4. Const References <ul><li>int main (int, char *[]) { </li></ul><ul><li>const int i = 0; </li></ul><ul><li>int j = 1; </li></ul><ul><li>// non-const reference </li></ul><ul><li>// r can’t refer to i </li></ul><ul><li>int &r = j; </li></ul><ul><li>// this is ok, though </li></ul><ul><li>const int &s = i; </li></ul><ul><li>const int &t = j; </li></ul><ul><li>} </li></ul><ul><li>Remember: references must refer to something </li></ul><ul><ul><li>Can’t be 0 (or NULL) </li></ul></ul><ul><ul><li>Except through bad tricks like </li></ul></ul><ul><ul><li>int *p = 0; int & r = *p; </li></ul></ul><ul><li>Also, once initialized, references cannot be changed </li></ul><ul><ul><li>E.g., can’t redirect t to i </li></ul></ul><ul><ul><li>In Java, can re-assign references </li></ul></ul><ul><ul><li>In C++, you cannot </li></ul></ul><ul><li>Const reference </li></ul><ul><ul><li>Promise not to change what’s aliased </li></ul></ul><ul><ul><li>E.g., can’t use t to change j </li></ul></ul><ul><li>Can’t have a non-const reference alias a const variable </li></ul><ul><ul><li>Reverse is OK </li></ul></ul>
  5. 5. Exercise <ul><li>Write a simple main function </li></ul><ul><ul><li>or just use the one provided in the array_exercise0.cc file </li></ul></ul><ul><li>Declare two integers </li></ul><ul><ul><li>Give them different initial values </li></ul></ul><ul><ul><li>Make one of them const </li></ul></ul><ul><li>Declare four references </li></ul><ul><ul><li>One const and one non-const reference initialized to alias each of the integers </li></ul></ul><ul><ul><li>What happens when you compile? </li></ul></ul><ul><ul><li>Comment out any reference declaration that’s illegal </li></ul></ul><ul><ul><li>What is printed when you insert print the names and values of the variables and the (remaining) references into cout? </li></ul></ul><ul><li>Use a non-const reference to change a value </li></ul><ul><ul><li>What is printed when you for the same variables and references now? </li></ul></ul><ul><li>Do you understand why it prints what it prints each time? </li></ul>
  6. 6. What’s a Pointer in C++? <ul><li>A variable holding an address </li></ul><ul><ul><li>Of what it “points to” in memory </li></ul></ul><ul><li>Can be untyped </li></ul><ul><ul><li>void * v; // can point to any type </li></ul></ul><ul><li>However, usually they’re typed </li></ul><ul><ul><li>Checked by compiler </li></ul></ul><ul><ul><li>Can only be assigned addresses of variables of the type to which it can point </li></ul></ul><ul><ul><li>int * p; // can only point to int </li></ul></ul><ul><li>Addresses: garbage, something, nothing </li></ul><ul><ul><li>When created: int * p = i; vs. int * q; </li></ul></ul><ul><ul><li>q = 0; // now it points to nothing </li></ul></ul><ul><ul><li>p = NULL; // not portable, use 0 instead </li></ul></ul>0x7fffdad0 7 int i int * p 3 const int j 7 int k
  7. 7. Location and Value Comparisons <ul><li>Pointers may be compared for equality </li></ul><ul><ul><li>Same as comparing addresses of what pointers point to (memory locations: l-values) </li></ul></ul><ul><li>Contents of what pointers point to may be compared, too (r-values) </li></ul><ul><li>First implies second, not other way around </li></ul>p == q &*p == &*q *p == *q 0xefffdad0 5 int i int *p 5 int j 0xefffdbd0 int *q
  8. 8. Const Pointers and Pointers to Const Types <ul><li>int main (int, char *[]) { </li></ul><ul><li>const int i = 0; </li></ul><ul><li>int j = 1; </li></ul><ul><li>int k = 2; </li></ul><ul><li>// pointer to int </li></ul><ul><li>int * w = &j; </li></ul><ul><li>// const pointer to int </li></ul><ul><li>int * const x = &k; </li></ul><ul><li>// pointer to const int </li></ul><ul><li>const int * y = &i; </li></ul><ul><li>// const pointer to const int </li></ul><ul><li>const int * const z = &j; </li></ul><ul><li>} </li></ul><ul><li>Make promises via the const keyword in pointer declaration: </li></ul><ul><ul><li>not to change pointer itself </li></ul></ul><ul><ul><li>not to change value it aliases </li></ul></ul><ul><ul><li>can also promise neither/both </li></ul></ul><ul><li>Read declarations right to left </li></ul><ul><li>In this example, can change </li></ul><ul><ul><li>w and what it points to </li></ul></ul><ul><ul><li>what x points to but not x </li></ul></ul><ul><ul><li>y but not what it points to </li></ul></ul><ul><ul><li>neither z nor what it points to </li></ul></ul><ul><li>A pointer to a non-const type cannot point to a const variable </li></ul><ul><ul><li>w and x can’t point to i </li></ul></ul>
  9. 9. Exercise <ul><li>Write another simple main function </li></ul><ul><li>Declare two integers </li></ul><ul><ul><li>Give them different initial values </li></ul></ul><ul><ul><li>Make one of them const </li></ul></ul><ul><li>Declare four pointers, all initialized to 0 </li></ul><ul><ul><li>Make one of them a pointer to integer </li></ul></ul><ul><ul><li>Make one of them a const pointer to integer </li></ul></ul><ul><ul><li>Make one of them a pointer to const integer </li></ul></ul><ul><ul><li>Make one of them a const pointer to const integer </li></ul></ul><ul><li>Try assigning addresses of integer variables </li></ul><ul><ul><li>Which pointers cannot change at all? </li></ul></ul><ul><ul><li>Which pointer can point to either variable? </li></ul></ul><ul><ul><li>Which pointer can only point to one of the variables? </li></ul></ul><ul><li>As you change the pointers </li></ul><ul><ul><li>Print out their names, addresses, and values of what they point to </li></ul></ul>

×