zaPointer Lecture 1 - Monday November 15 th...

Info icon This 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!
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 ;      ...
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
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?
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 .”
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 ... }
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 ...
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern