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

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

View Full Document Right Arrow Icon
5/12/2009 1 Struct and Enum Ve 280 Programming and Introductory Data Structures Structs and Enums Pointer Arithmetic Recall y Remember that both of the following expressions are examples of pointer arithmetic: array[3] *(array+3) y Recall that an address is a location in memory. y Each byte in memory has a distinct address – that is, C++ is byte- addressable. y Every type has a “size” associated with it; the number of bytes of memory required to hold an object of that type. y The language standard does not guarantee much in the way of sizes. y For example, an “int” could be four bytes with one version of the compiler, and eight bytes on another version. Memory Addressability Type sizes y The amount of memory assigned to a data type is a source of innumerable “portability bugs” in programs. y 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. y There are some guarantees, however: y A “char” is always one byte. y A “short” is always at least as big as a char. y An int is always at least as big as a short. y A long is always at least as big as an int. y 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 y 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. y 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 y Now, recall that if a is an array, and i is an int, the following two expressions are equivalent: a[i] *(a + i) y This seems strange, because the elements in ac and ai are different sizes! The int ai[2]; char ac[2]; 100: ai[0] 104: ai[1] intuition is a+i should be a pointer value that points to the object a[i] . y 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. 108: ac[0] 109: ac[1] 110: ????? Using Arrays Product Types y Write a program that keeps track of student grades. y For each student we need to know: 1. The student’s name 2. The midterm grade 3. The final exam grade y We could have three arrays: string names[233]; int midterm[233]; int final[233]; y 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 DocumentRight Arrow Icon
5/12/2009 2 On to Structs Product Types y Instead of three separate arrays, what we really want is a type that can “bind together” several other types into one “meta-type”. y
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.

This note was uploaded on 10/26/2009 for the course EECS 280 taught by Professor Jeff during the Summer '09 term at University of Michigan-Dearborn.

Page1 / 6

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

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