lecture12%2Dpost

# lecture12%2Dpost - Wednesday,November17th...

This preview shows pages 1–8. 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
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.
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 ?

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

View Full Document
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).
#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]

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

View Full Document
Pointers and Arrays main() { short arr[4] = {-1,-2,-3,-4}; short *parray; cout << arr[0] << endl; cout << &arr[0] 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
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 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.

## This note was uploaded on 04/10/2012 for the course CS 31 taught by Professor Melkanoff during the Winter '00 term at UCLA.

### Page1 / 30

lecture12%2Dpost - Wednesday,November17th...

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

View Full Document
Ask a homework question - tutors are online