{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture3CS32Nachenburg

# lecture3CS32Nachenburg - 1 Fr i day J an 8 th A Qui ck Revi...

This preview shows pages 1–11. Sign up to view the full content.

1 Friday, Jan 8 th A Quick Review of Pointers Dynamic Arrays Resource Management Part 1 Copy Constructors Note: We’re only reviewing about 50% of the key pointer concepts! If you feel uncomfortable with pointers, then study and become an expert before our next class! Carey’s CS31 Pointer notes: posted onto our website! cs31.zip: lecture11.ppt , lecture12.ppt , lecture13.ppt

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

View Full Document
2 Let’s Play…. Programming Language Inventor Or Serial Killer See if you can guess who uses a keyboard and who uses a chainsaw!
3 Every Variable has an Address You can think of the computer’s memory like a street with a bunch of vacant lots. Some variables occupy a single lot, while others occupy several adjacent lots. 100 101 102 103 104 MEMORY void foo(void) { short boogers = 5; char mucus = ‘G’; ... boogers 5 mucus ‘G’ When you define a variable in your program, the computer finds an unused address in memory and reserves it for your variable.

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

View Full Document
4 Variable Addresses Important : The address of a variable is defined to be the first address in memory where the variable is stored. 00000000 00000001 00001000 00001001 00001002 00001003 00001004 00001005 00001006 00001007 00001008 00001009 00001010 00001011 99999990 99999991 99999992 So, what is vomit’s address in memory? int main(void) { int vomit = 15; char booger = ‘B’; 15 66 What about booger’s address?
5 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 MEMORY

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

View Full Document
6 I have an address… now what? Once you get the address of a variable, what do you do with it? Well, when you ask a girl/ guy for their address, what do you do with it? You write it down in your little black book! In a similar way, once we get the address of a variable, we save it in a pointer variable . 100 De Neve Westwood, CA
7 Defining a Pointer Variable A pointer variable is a variable that holds an address rather than a normal int , float , double or string value. You can set a pointer variable equal to another variable’s address: int main(void) { short Jennifer; pointer blackBook; blackBook = & Jennifer; ... ... 00001000 00001001 00001002 00001003 00001004 00001005 00001006 00001007 ... Jennifer blackBook 1000 / / blackBook now holds Jenny’s / / address

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

View Full Document
8 Defining a Pointer Variable Actually… This isn’t the correct way to define a pointer variable. int main(void) { short Jennifer; blackBook = & Jennifer; ... When you define a pointer, you have to tell C++ what type of variable it’s going to point to. pointer blackBook; short Oh, and in C++, you use the * symbol instead of the word “pointer.” So here’s the right * blackBook;
9 More Pointer Examples… int main(void) { float diarrhea = 3.1415; * ptr; / / points to float variables ptr = & diarrhea; / / ptr gets diarrhea’s address string barf = “blah”; string * ptrToStr; / / points to string variables ptrToStr = & barf; int eek; int * ptr1, * ptr2; / / points to int variables ptr1 = & eek; ptr2 = ptr1; / / now ptr2 points to eek too!

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

View Full Document
10 Using Pointer Variables…
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 68

lecture3CS32Nachenburg - 1 Fr i day J an 8 th A Qui ck Revi...

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

View Full Document
Ask a homework question - tutors are online