LEC20081017

# LEC20081017 - Introduction to Computer Programming CSC180...

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

Introduction to Computer Programming October 17, 2008 CSC180 Fall 2008, University of Toronto

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

View Full Document
Pointer arithmetic Pointers and arrays are strongly related. When pointers point to an array, we can: 1. Add a pointer and an integer 2. Subtract a pointer and an integer 3. Subtract two pointers 4. Increment and decrement a pointer using ++ and -- , respectively 5. Compare two pointers Example: int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; CSC180 Fall 2008, University of Toronto 1
int x, i; int *pa, *qa; pa = &a[0]; x = *pa; if (*(pa + 1) == a[1]) printf(‘‘*(pa + 1) == a[1]: yes\n’’); i = 5; if (*(pa + i) == a[i]) printf(‘‘*(pa + %d) == a[%d]: yes\n’’, i, i); pa = &a[5]; i = 4; if (*(pa - i) == a[1]) printf(‘‘*(pa %d) == a[1]: yes\n’’, -i); pa = a; if (*pa == a[0]) printf(‘‘*pa == a[0]: yes\n’’); CSC180 Fall 2008, University of Toronto 2

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

View Full Document
qa = &a[5]; printf(‘‘qa - pa: %d\n’’, qa - pa); printf(‘‘pa - qa: %d\n’’, pa - qa); printf(‘‘qa > pa: %d\n’’, qa > pa);
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/10/2011 for the course CSC 180 taught by Professor Na during the Fall '01 term at University of Toronto.

### Page1 / 12

LEC20081017 - Introduction to Computer Programming CSC180...

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

View Full Document
Ask a homework question - tutors are online