08+-+Structs+and+Enums

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

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

Structs and Enums EECS 280 Programming and Introductory Data Structures

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

View Full Document
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
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.

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

View Full Document
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
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: ?????

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

View Full Document
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];
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:

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

View Full Document
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
Ask a homework question - tutors are online