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

Info iconThis 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 s Create a pointer by saying: type* pointer_name; string* my_ptr; //Creates pointer to string block. Points to nothing. s string name = "FRODO"; my_ptr = &name; //my_ptr now points to the string block. s Dereferencing : we can get the value it points to using * . cout << *my_ptr; //Prints FRODO *my_ptr = "GANDALF"; //Changes string to GANDALF s We can free up memory with the delete function. "Deletes" the memory block, not the pointer. delete my_ptr; //Deallocates the string block. s We can allocate memory with the new function. string* gollum_ptr = new string; //Creates empty string block. *gollum_ptr = "GOLLUM"; //Block now stores GOLLUM
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Pointers in Functions s Just like other variables, pointers can be passed by value or reference. s 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. int main() {
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/27/2010 for the course PIC 157-050-21 taught by Professor Wittman during the Fall '10 term at UCLA.

Page1 / 6

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

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

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