lecture3CS32Nachenburg

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

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

View Full Document Right Arrow Icon
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
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 Let’s Play…. Programming Language Inventor Or Serial Killer See if you can guess who uses a keyboard and who uses a chainsaw!
Background image of page 2
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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
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?
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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;
Background image of page 8
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!
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 Using Pointer Variables…
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/11/2010 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Page1 / 68

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

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

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