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

Info icon This 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
Image of page 1

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

View Full Document Right 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
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
Image of page 3

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

View Full Document Right 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);
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
Image of page 5

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern