{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lec22 - Lecture 22 Practice With Pointers PIC 10A Todd...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 22: Practice With Pointers PIC 10A Todd Wittman Review of Pointers square6 Create a pointer by saying: type* pointer_name; string* my_ptr; //Creates pointer to string block. Points to nothing. square6 A pointer stores the address (&) of a memory block. string name = "FRODO"; my_ptr = &name; //my_ptr now points to the string block. square6 Dereferencing : we can get the value it points to using * . cout << *my_ptr; //Prints FRODO *my_ptr = "GANDALF"; //Changes string to GANDALF square6 We can free up memory with the delete function. "Deletes" the memory block, not the pointer. delete my_ptr; //Deallocates the string block. square6 We can allocate memory with the new function. string* gollum_ptr = new string; //Creates empty string block. *gollum_ptr = "GOLLUM"; //Block now stores GOLLUM
Image of page 1

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

View Full Document Right Arrow Icon
2 Pointers in Functions square6 Just like other variables, pointers can be passed by value or reference. void my_function (int* p, int* &q) square6 When a pointer is passed by value (no &), we will not record changes to the address. However, it could change the value it is pointing to.
Image of page 2
Image of page 3
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