08+-+Structs+and+Enums

08+-+Structs+and+Enums - EECS 280 Programming and...

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

View Full Document Right Arrow Icon
Structs and Enums EECS 280 Programming and Introductory Data Structures
Background image of page 1

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

View Full DocumentRight Arrow Icon
Pointer Arithmetic Recall Remember that both of the following expressions are examples of pointer arithmetic: array[3] *(array+3) 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
Background image of page 2
Memory Addressability Type sizes The amount of memory assigned to a data type is a source of innumerable “portability bugs” in programs. 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. There are some guarantees, however: A “char” is always one byte.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Memory Addressability Type sizes 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. This is very useful for pointer arithmetic. Suppose we have two
Background image of page 4
Memory Addressability Type sizes 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 int ai[2]; char ac[2]; 100: ai[0] 104: ai[1] 108: ac[0] 109: ac[1] 110: ?????
Background image of page 5

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

View Full DocumentRight Arrow Icon
Using Arrays Product Types Write a program that keeps track of student grades. For each student we need to know: 1. The student’s name 2. The midterm grade 3. The final exam grade We could have three arrays: string names[233]; int midterm[233];
Background image of page 6
Product Types Instead of three separate arrays, what we really want is a type that can “bind together” several other types into one “meta-type”. This is called a “product” type and it describes a “compound object” that comprises one or more elements, each of independent type. C++ supports a product type:
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/19/2012 for the course EECS 280 taught by Professor Noble during the Winter '08 term at University of Michigan.

Page1 / 31

08+-+Structs+and+Enums - EECS 280 Programming and...

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

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