lecture12%2Dpost

lecture12%2Dpost - Wednesday,November17th BasicPointerReview

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

View Full Document Right Arrow Icon
Wednesday, November 17 th Basic Pointer Review Pointers and Arrays Pointer Arithmetic Iterating Through an Array W/Pointers Passing Arrays to Functions: The Truth!
Background image of page 1

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

View Full DocumentRight Arrow Icon
What does the following program print out?  Pointer Review  main() { int i1 = 100, i2; int *pi; pi = & i1; cout << i1; cout << *pi; *pi = 10; cout << i1; i2 = *pi; cout << i2; } Assume i1 is at address 1000, i2 at  1004, and pi at 1008  Let’s solve this one on the chalk board.
Background image of page 2
What does the following program print out?  Pointer Review #2 int * wedgie(void) { static int foo = 7; cout << foo; return(&foo); } main() { int *blah; blah = wedgie(); cout << *blah; (*blah) += 3; wedgie(); } Let’s also solve this one on the chalk  board. Part #2: What if our variable foo is  not  static ?
Background image of page 3

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

View Full DocumentRight Arrow Icon
Pointers and Arrays In C++, we often use pointers with arrays.  Let’s learn six  different techniques to help us do this: 1. How to get the address of an array variable. 2. How to index the elements of an array using pointers and  brackets. 3. What happens if you increment or decrement a pointer variable?  (Pointer Arithmetic) 4. Using the * operator with arrays. 5. Iterating through an array with pointers. 6. Passing an array to a function (using pointers).
Background image of page 4
#1: Getting the  address  of the start of  an array Technique #2 : main() { short arr[4] = {-1,-2,-3,-4}; cout << arr ; Technique #1 : main() { short arr[4] = {-1,-2,-3,-4}; cout << ; // get addr 00001000 00001001 00001002 00001003 00001004 00001005 00001006 00001007 00001008 00001009 00001010 00001011 arr               -1 -2 -3 -4 [0] [1] [2] [3]
Background image of page 5

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

View Full DocumentRight Arrow Icon
Pointers and Arrays main() { short arr[4] = {-1,-2,-3,-4}; short *parray; cout << arr[0] << endl; cout << &arr[0] << endl; cout << arr << endl; parray = arr ; // OR: parray=&arr[0]; cout << parray << endl; } 00000000 00000001 00001000 00001001 00001002 00001003 00001004 00001005 00001006 00001007 00001008 00001009 00001010 00001011     parray  -1 1000 1000 arr               -1 -2 -3 -4 [0] [1] [2] [3] 1000 1000 If you refer to an array  without using  brackets  in a statement, the compiler replaces  this with the starting address of the array. &arr[0] -> 1000
Background image of page 6
You can use brackets with a pointer variable, just like you do  with an array: main() { int arr[3] = {5,6,7}; cout << arr[2]; ... main() { int arr[3] = {5,6,7}; int *ptr; ptr = arr; cout << ptr[2]; This  line  prints out the value that is  2   elements from the start of  arr .  
Background image of page 7

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

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

Page1 / 30

lecture12%2Dpost - Wednesday,November17th BasicPointerReview

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