{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs31-lecture12

# cs31-lecture12 - Wednesday,November17th BasicPointerReview...

This preview shows pages 1–10. Sign up to view the full content.

Wednesday, November 17 th Basic Pointer Review Pointers and Arrays Pointer Arithmetic Iterating Through an Array W/Pointers Passing Arrays to Functions: The Truth!

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

View Full Document
But first… Your favorite game!
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.

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

View Full Document
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 ?
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. 1. How to index the elements of an array using pointers and  brackets. 1. What happens if you increment or decrement a pointer variable?  (Pointer Arithmetic) 1. Using the * operator with arrays. 1. Iterating through an array with pointers. 1. Passing an array to a function (using pointers).

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

View Full Document
#1: Getting the  address  of the start of  an array Technique #2 : main() { short arr[4] = {-1,-2,-3,-4}; cout << arr ; // same as &arr[0] Technique #1 : main() { short arr[4] = {-1,-2,-3,-4}; cout << &arr[0] ; // get addr 00001000 00001001 00001002 00001003 00001004 00001005 00001006 00001007 00001008 00001009 00001010 00001011 arr               -1 -2 -3 -4 [0] [1] [2] [3]
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

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

View Full Document
#2 :Using Brackets with Pointers 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 .   This  line  prints out the value that is  2   elements from the start of where  ptr   points to.
Pointers and Arrays main() { int arr[3] = {5,6,7}; int *ptr; ptr = arr; cout << ptr[2]; } 00000000 00000001 00001000 00001001 00001002 00001003 00001004 00001005 00001006 00001007 00001008 00001009 00001010 00001011 00001012 00001013 00001014 00001015 arr 5 6 7         ptr  You can use  brackets  with a pointer, the same  way you use brackets with arrays!

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 31

cs31-lecture12 - Wednesday,November17th BasicPointerReview...

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

View Full Document
Ask a homework question - tutors are online