lecture3

lecture3 - Monday, April 10 vie rs A Quick Re w of Pointe...

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

View Full Document Right Arrow Icon
Monday, April 10 th A Quick Review of Pointers Getting the address of a variable Defining a pointer variable Dereferencing a pointer variable with the * operator Pointers and arrays Pointers and structs/classes New and Delete Introduction to Linked Lists Note: We don’t have time to do a full review of pointers! If you feel uncomfortable with pointers, then study and become an expert before our next class! Carey’s CS31 Pointer notes: groups.msn.com/uclacs31 lecture11-post.ppt , lecture12-post.ppt lecture13-post.ppt
Background image of page 1

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

View Full DocumentRight Arrow Icon
3 Examples… class someclass { public: someclass(int cnt) { m_ptr = new float[cnt]; } ~someclass() { delete [] m_ptr; } private: float *m_ptr; }; main() { someclass a(5); } // a is destructed class otherclass { otherclass(int val) m_value = val; } ~otherclass() { } m_value; { otherclass *ptr; ptr = new otherclass(5); delete ptr; { } { } { someclass *ptr; ptr = new someclass(5);
Background image of page 2
We can get the address of a variable using the & operator. Getting the Address of a Variable 01000 01001 01002 01003 01004 01005 01006 01007 01008 01009 01010 01011 vomit 15 booger ‘B’ int main(void) { int = 15; char = ‘B’; cout << “vomit’s address: “<< & vomit<<endl; cout << “booger’s address: “ << & booger; } If you place an & before a variable in your program, it means “ give me the numerical address of the variable .” Output: vomit’s address: 1006 booger’s address: 1011
Background image of page 3

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

View Full DocumentRight Arrow Icon
Defining a Pointer Variable A pointer variable is a variable that holds an address value rather than a normal int , float double or string value. You can set a pointer variable equal to a variable’s address: int main(void) { float diarrhea = 300; float * ptr; // a pointer variable ptr = &diarrhea; // ptr gets diarrhea’s address ... ... 00001000 00001001 00001002 00001003 00001004 00001005 00001006 00001007 ... diarrhea 300 ptr 1000
Background image of page 4
Defining a Pointer Variable… int main(void) { float diarrhea = 300; float * ptr; // ptr can point to float variables ptr = &diarrhea; // ptr gets diarrhea’s address To define a pointer variable, you use the following syntax: type * pointer_name; The * indicates that the variable is a pointer and not a regular variable. e.g. int * anIntPointer, * anotherOne; string * ptr_to_a_string; , notAPtr ;
Background image of page 5

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

View Full DocumentRight Arrow Icon
Using Pointer Variables… int main(void) { int boogies, nosehairs; boogies = 32; nosehairs = 15; int *ptr; cout << “boogies is at address “ << &boogies << endl; ptr = &boogies; cout << “ptr points to address “ << ptr << endl; ptr = &nosehairs; cout << “ptr points to address “ << ptr << endl; } 00000 00001 01000 01001 01002 01003 01004 01005 01006 01007 01008 01009 01010 01011 boogies nosehairs 32 15 ptr ? boogies is at address 1000 1000 ptr points to address 1000 1004 Yo Mamma!
Background image of page 6
int main(void) { int boogies, nosehairs; boogies = 32; nosehairs = 15; int *ptr; cout << “boogies is at address “ << &boogies << endl; ptr = &boogies; cout << “ptr points to address “ << ptr << endl; ptr = &nosehairs; cout << “ptr points to address “ << ptr << endl; } 00000 00001 01000 01001 01002 01003 01004 01005 01006 01007 01008 01009 01010 01011 boogies nosehairs 32 15 ptr boogies is at address 1000 ptr points to address 1000 1004 ptr points to address 1004 Using Pointer Variables…
Background image of page 7

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

View Full DocumentRight Arrow Icon
main() { float fv = 3.14; float *pfv; pfv = &fv; cout << “value: “ << pfv << endl; cout << “size: "<<sizeof(pfv)<<endl; cout << "address: " << &pfv << endl; } 00000000 00000001 00009240 00009241 00009242 00009243 00009242 00009245 00009246 00009247 00009248 00009249 00009250 00009251 pfv fv 3.14 9241 value: 9241 size: 4 address: 9245 Using Pointer Variables…
Background image of page 8
Using The * Operator main() { short eyeballs = 2; short *ptr;
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/20/2009 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Page1 / 45

lecture3 - Monday, April 10 vie rs A Quick Re w of Pointe...

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

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