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
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 sexpressions, tree representations Mergesort and binary search Hash tables (implementation and use) CS 136 Winter 2009 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 4byte int , the compiler generates machine instructions to do the multiplication before the addition. CS 136 Winter 2009 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, A[3] = {9, 2, 5}; p = A; q = A; q++; After the execution of q++ : p is a pointer to the element of A of index 0 q is a pointer to the element of A of index 1 CS 136 Winter 2009 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 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 2009 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 2009 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. It is still idiomatic to use pointer arithmetic in C programs, especially in the manipulation of strings, which can be viewed as arrays of characters. We will gain practice with the syntax in this setting....
View
Full
Document
This note was uploaded on 08/31/2011 for the course CS 136 taught by Professor Becker during the Fall '08 term at Waterloo.
 Fall '08
 BECKER
 Algorithms, Binary Search, Data Structures, Sort

Click to edit the document details