08-Arrays-The-Full-Story

08-Arrays-The-Full-Story - CS107 Spring 2007 Handout 08...

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

View Full Document Right Arrow Icon
CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays This handout was written by Nick Parlante and Julie Zelenski. C Arrays As you recall, a C array is formed by laying out all the elements contiguously in memory from low to high. The array as a whole is referred to by the address of the first element. For example, the variable intArray below is synonymous with the address of the first element and can be used in expressions like an int * . int intArray[6]; intArray[0] ... intArray[1] The programmer can refer to elements in the array with the simple [] syntax such as intArray[1] . This scheme works by combing the base address of the array with the index to compute the base address of the desired element in the array, using some simple arithmetic. Each element takes up a fixed number of bytes known at compile- time. So address of the n th element in the array ( 0 -based indexing) will be at an offset of ( n * element_size ) bytes from the base address of the whole array. address of nth element = address_of_0th_element + (n * element_size_in_bytes) The square bracket syntax [] deals with this address arithmetic for you, but it's useful to know what it's doing. The [] multiplies the integer index by the element size, adds the resulting offset to the array base address, and finally dereferences the resulting pointer to get to the desired element. intArray[3] = 13; [0] [1] [2] [3] [4] [5] 0 4 8 12 16 20 index Offset in bytes Assume sizeof(int) = 4. Each element takes up 4 bytes. 13 base + 3*elem_size
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
2 '+' Syntax In a closely related piece of syntax, adding an integer to a pointer does the same offset computation, but leaves the result as a pointer. The square bracket syntax dereferences that pointer to access the nth element while the + syntax just computes the pointer to the nth element. So the expression ( intArray + 3 ) is a pointer to the integer intArray[3] . ( intArray + 3 ) is of type ( int* ) while intArray[3] is of type int . The two expression only differ by whether the pointer is dereferenced or not. So the expression ( intArray + 3 ) is exactly equivalent to the expression ( ) . In fact those two probably compile to exactly the same code. They both represent a pointer to the element at index 3 . Any [] expression can be written with the + syntax instead. We just need to add in the pointer dereference. So intArray[3] is exactly equivalent to * ( intArray + 3 ) . For most purposes, it's easiest and most readable to use the [] syntax. Every once in a while the + is convenient if you needed a pointer to the element instead of the element itself. Pointer++
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

08-Arrays-The-Full-Story - CS107 Spring 2007 Handout 08...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online