{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


08+-+Structs+and+Enums - Pointer Arithmetic Recall Remember...

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

View Full Document Right Arrow Icon
9/1/10 1 Structs and Enums EECS 280 Programming and Introductory Data Structures 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 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. 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. A “short” is always at least as big as a char. An int is always at least as big as a short. A long is always at least as big as an int. 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 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 arrays, one of chars and one of ints. (assume ints are four bytes long.) int ai[2]; char ac[2]; 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 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 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]; int final[233]; 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
9/1/10 2 On to Structs 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”.
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 ]}

Page1 / 6

08+-+Structs+and+Enums - Pointer Arithmetic Recall Remember...

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

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