{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture11%2Dpost

lecture11%2Dpost - Monday November 15 th Nomidtermgradesyet...

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

View Full Document Right Arrow Icon
Monday, November 15 th No  midterm grades yet… Pointers pointers pointers Pointers are one of the  most complex  topics in C/C++. If you  don’t understand pointers well, you’ll be tortured by  horrible  bugs , so pay attention!
Background image of page 1

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

View Full Document Right Arrow Icon
And now for… Pointers Every variable in C++ can be described by  five    different attributes:     1. every variable has a name  2. every varaible has a type 3. every variable has a size 4. every variable has a value 5. every variable has an address! int main(void) {       int   earwax  =   3 ;      ...
Background image of page 2
Variable Addresses Every byte in the computer‘s memory has its own   address  ( just like each house on a street has an  address ).   00000000 00000001 00001000 00001001 00001002 00001003 00001004 00001005 00001006 00001007 00001008 00001009 00001010 00001011 99999990 99999991 99999992 When you define a variable in your program, the  computer finds an unused set of address in memory and  reserves them for your variable. 1 byte int main(void) {     int     vomit  = 15;     char  booger  = ‘B’; vomit 15 booger 66
Background image of page 3

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

View Full Document Right Arrow Icon
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’; vomit 15 booger 66 What about  booger’s  address?
Background image of page 4
We can get the address of a variable using the  operator.  Finding the Address of a Variable 00001000 00001001 00001002 00001003 00001004 00001005 00001006 00001007 00001008 00001009 00001010 00001011 vomit 15 booger 66 int main(void) {     int     vomit  = 15;     char  booger  = ‘B’;     cout << “Value: “ << vomit << endl;     cout << “Size: “ << sizeof(vomit) << endl;     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 .”
Background image of page 5

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

View Full Document Right Arrow Icon
&: It’s for References and Pointers When we place an & before a function parameter , we make the parameter a  reference parameter . void booboo(int & a) { ... // a is a reference parameter. } Be careful not to confuse the two. When we place an & before a variable in a program statement, we are asking  for that variable’s  address . int main(void) { int a; cout << & a; // get a’s address ... }
Background image of page 6
void change_me(int x); int main(void) { int n; cout << "Enter a #: "; cin >> n; cout << n << endl; change_me(n); cout << n << endl; } // definition void change_me(int x) { x = 12; } Output: Enter a #:  n 3 3 3 x 3 12 3 Every Variable Has An Address ...
Background image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}