08-algds-post3up

# 08-algds-post3up - More algorithms and data structures...

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

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.

Unformatted text preview: More algorithms and data structures Readings: Readings from CP:AMA, as detailed in the document available on the Web site. • Pointer arithmetic and string manipulation • Reading s-expressions, tree representations • Mergesort and binary search • Hash tables (implementation and use) CS 136 Winter 2011 08: More algorithms and data structures 1 Pointer arithmetic int my_function( int * A, int n) { ... } In the body of the function, A can be treated as an array or as a pointer to an integer. This is true even if int A is used. The address of A[i] is computed by multiplying i by 4 and adding the address referenced by A . We can also perform this address computation explicitly in C with the expression A + i . Because A is a pointer to a 4-byte int , the compiler automatically multiplies i by 4. If the array held person structures where sizeof ( struct person) is 20, then i would be automatically multiplied by 20. CS 136 Winter 2011 08: More algorithms and data structures 2 The expression * (A+i) thus has the same meaning as A[i] . They are identical as lvalues and rvalues. int * p, * q; int A[3] = {9, 2, 5}; A points to the first integer in an array of 3 inte- gers p = A; p points to the first integer in array A p[0] = 3; A[0] is now 3 q = p + 1; q now points to integer after the one p points to (2). q[0] is 2, q[1] is 5, q[-1] is 3. *q has the same meaning as q[0]. CS 136 Winter 2011 08: More algorithms and data structures 3 252 9 2 5 244 248 252 256 address contents p 240 identifer location q 244 248 240 A 248 q - p gives the number of elements between p and q . It has value 1, even though the addresses of these two elements differ by 4. This makes sense, as pointer subtraction should “undo” addition to a pointer. The comparison q < p yields 0 ( false ). CS 136 Winter 2011 08: More algorithms and data structures 4 The only arithmetic operations permitted with pointers are: • adding an integer to a pointer, producing a pointer value; • subtracting an integer from a pointer, producing a pointer value; • subtracting a pointer from a pointer of the same type, producing an integer value. We can use array notation with pointers, if we wish. Since p points to the element of A of index 0: A[0] , * (A+0) , * A , * p , * (p+0) , and p[0] all have the same meaning. Since q points to the element of A of index 1: A[1] , * (A+1) , * q , * (q+0) , and q[0] all have the same meaning. CS 136 Winter 2011 08: More algorithms and data structures 5 Motivation for pointer arithmetic Explicit arithmetic on pointers was originally used to steer the compiler towards producing a more efficient translation into machine code. Modern compilers can perform optimizations that can generate identical code whether pointer arithmetic or array notation is used....
View Full Document

## This note was uploaded on 10/02/2011 for the course CS 136 taught by Professor Becker during the Spring '08 term at Waterloo.

### Page1 / 29

08-algds-post3up - More algorithms and data structures...

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

View Full Document
Ask a homework question - tutors are online