{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

08-algds-post3up

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

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
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 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 4-byte 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
Background image of page 1

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

View Full Document Right Arrow Icon
252 9 2 5 244 248 252 256 address contents p 240 identifier 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. CS 136 Winter 2009 08: More algorithms and data structures 6
Background image of page 2
Strings A string of length n in C is actually an array of n + 1 characters, where the character of index n is the special null character represented by ’\0’ .
Background image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}