C++_pointers_and_references

C++_pointers_and_references - Motivation and Overview Often...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 332: C++ Pointers, Arrays and References Motivation and Overview Often need to refer to another object Without making a copy of the object itself Two ways to do this Indirectly, via a pointer Gives the address of the object (analogy: street address) Requires the code to do extra work: dereferencing Like going to the given address, to talk to the person Directly, via a reference Acts as an alias for the object Code interacts with reference as if it were object itself
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 332: C++ Pointers, Arrays and References Review: What’s a Pointer? A variable holding an address Of what it “points to” in memory Can be untyped E.g., void * v; // points to anything However, usually they’re typed Checked by compiler Can only be assigned addresses of variables of type to which it can point E.g., int * p; // only points to int Can point to nothing E.g., p = 0; // points to nothing Can change where it points As long as pointer itself isn’t const E.g., p = &i; // now points to i 0x7fffdad0 7 int i int *p
Background image of page 2
CSE 332: C++ Pointers, Arrays and References Review: What’s a Reference? Also a variable holding an address Of what it “refers to” in memory But with a nicer interface A more direct alias for the object Hides indirection from programmers Must be typed Checked by compiler Again can only refer to the type with which it was declared E.g. , int & r =i; // refers to int i Always refers to (same) something Must initialize to refer to a variable Can’t change what it aliases 0x7fffdad0 7 int i int & r
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 332: C++ Pointers, Arrays and References Untangling Operator Syntax Symbol Used in a declaration Used in a definition unary (ampersand) reference, e.g., int i; int &r = i; address-of, e.g., p = & i; unary * (star) pointer, e.g., int * p; dereference, e.g., * p = 7; -> (arrow) member access via pointer, e.g., C c; C * cp=&c; cp->add(3); . (dot) member access via reference or object, e.g. , C c; c.add(3); C & cr = c; cr.add(3);
Background image of page 4
CSE 332: C++ Pointers, Arrays and References Aliasing and Pointers int main (int argc, char *argv[]) { int i = 0; int j = 1; int * p = & i; int * q = & i; *q = 6; // i is now 6, j is still 1 } Distinct variables have different memory locations E.g. , i and j A variable and all the pointers to it (when they’re dereferenced ) all alias the same location E.g. , i , *p , and *q Assigning a new value to i , *p or *q changes value seen through the others But does not change value seen through j 0xefffdad0 6 int i int *p 1 int j 0xefffdad0 int *q
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 332: C++ Pointers, Arrays and References Review: Const Pointers
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/19/2011 for the course CSE 332S taught by Professor Smart during the Spring '08 term at Washington University in St. Louis.

Page1 / 17

C++_pointers_and_references - Motivation and Overview Often...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online