lecture9_structs-enums

lecture9_structs-enums - Last time: * the array/pointer...

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

View Full Document Right Arrow Icon
Last time: * the array/pointer duality * traversing arrays with pontrs. Today: * Product types * C++ structs * Enumerations Recall that an *address* is a location in memory. Each byte in memory has a distinct address---that is, C++ is byte-addressable. Every type has a "size" associated with it; the number of bytes of memory required to hold an object of that type. The language standard does not guarantee much in the way of sizes. For example, an "int" could be four bytes with one version of the compiler, and eight bytes on another version. Since the sizes 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. This is very useful for pointer arithmetic. Suppose we have two arrays, one of chars and one of ints. (For the moment, let's assume ints are four bytes long.) int ai[2]; char ac[2]; Now, recall that if "a" is an array, and "i" is an int, the following two expressions are equivalent: a[i] *(a + i) 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]. 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. ++++++++++++++++++++++++++++++++++++++++++++++++++ Product types. Suppose I wanted to write a program that kept track of students
Background image of page 1

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

View Full DocumentRight Arrow Icon
grades. For each student I need to know: The student's name The midterm grade The final exam grade I could have three arrays: string names[233]; int midterm[233]; int final[233]; But, it is not immediately clear that these arrays are related to one another, or that midterm[5] is the midterm grade belonging to the student named by names[5]. What we really want is a type that can "bind together" several other types into one "meta-type". This is called a "product" type: A product type describes a "compound object" that comprises one or more elements, each of independent type. C++ supports a product type: a "struct", short for structure.
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 04/04/2008 for the course EECS 215 taught by Professor Phillips during the Winter '08 term at University of Michigan.

Page1 / 7

lecture9_structs-enums - Last time: * the array/pointer...

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