cs31-lecture11

cs31-lecture11 - Monday, November 15 th No midterm grades...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Monday, November 15 th No midterm grades yet Pointers , pointers , pointers Pointers are one of the most complex topics in C/ C++. I f you dont understand pointers well, youll be tortured by horrible bugs , so pay attention! 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 ; ... Variable Addresses Every byte in the computers 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 Variable Addresses I mportant : 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 vomits address in memory? int main(void) { int vomit = 15; char booger = B; vomit 15 booger 66 What about boogers address? 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 << Vomits address: << & vomit<<endl; cout << boogers address: << & booger; } I f you place an & before a variable in your program, it means give me the numerical address of the variable . &: Its 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 r efer ence par ameter . } Be car eful not to confuse the two. When we place an & before a variable in a program statement, we are asking for that variables address . int main(void) { int a; cout << & a; // get as addr ess ... } 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 ......
View Full Document

Page1 / 28

cs31-lecture11 - Monday, November 15 th No midterm grades...

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

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