{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

08+-+Structs+and+Enums - Pointer Arithmetic Recall EECS 280...

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

View Full Document Right Arrow Icon
10/4/2009 1 Structs and Enums EECS 280 Programming and Introductory Data Structures Pointer Arithmetic Recall circle5 Remember that both of the following expressions are examples of pointer arithmetic: array[3] *(array+3) circle5 Recall that an address is a location in memory. circle5 Each byte in memory has a distinct address – that is, C++ is byte- addressable. circle5 Every type has a “size” associated with it; the number of bytes of memory required to hold an object of that type. circle5 The language standard does not guarantee much in the way of sizes. circle5 For example, an “int” could be four bytes with one version of the compiler, and eight bytes on another version. Memory Addressability Type sizes circle5 The amount of memory assigned to a data type is a source of innumerable “portability bugs” in programs. circle5 For example, suppose someone writes a program that assumes that all “ints” are 8 bytes long. If that program is compiled on a 4- byte-int compiler, it is likely to break, if it compiles at all. circle5 There are some guarantees, however: circle5 A “char” is always one byte. circle5 A “short” is always at least as big as a char. circle5 An int is always at least as big as a short. circle5 A long is always at least as big as an int. circle5 However, while a “char” is always one byte, the restrictions on byte are strange: it must have at least eight bits, but could have more! Memory Addressability Type sizes circle5 Since the sizes of types are not fixed, C++ provides an operator that you can use to determine the size of something, in bytes. For example: sizeof(int) tells you the number of bytes required to store an int. circle5 This is very useful for pointer arithmetic. Suppose we have two arrays, one of chars and one of ints. (assume ints are four bytes long.) int ai[2]; char ac[2]; Memory Addressability Type sizes circle5 Now, recall that if a is an array, and i is an int, the following two expressions are equivalent: a[i] *(a + i) circle5 This seems strange, because the elements in ac and ai are different sizes! The intuition is a+i should be a pointer value that points to the object a[i] . circle5 The sizeof() operator makes this happen. Since all pointers point to some specific type, and the compiler knows how big each type is, it can add the appropriate value to the pointer to get the right answer. int ai[2]; char ac[2]; 100: ai[0] 104: ai[1] 108: ac[0] 109: ac[1] 110: ????? Using Arrays Product Types circle5 Write a program that keeps track of student grades. circle5 For each student we need to know: 1. The student’s name 2. The midterm grade 3. The final exam grade circle5 We could have three arrays: string names[233]; int midterm[233]; int final[233]; circle5 However, it isn’t clear the arrays are related or that midterm[5] is the midterm grade belonging to the student named by names[5].
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
10/4/2009 2 On to Structs Product Types circle5 Instead of three separate arrays, what we really want is a type that can “bind together” several other types into one “meta-type”.
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 ]}